0

嗨,我正在解析 xml 标签而不使用任何解析器,只使用 StringUtils.substring,因为我只需要 2 个标签值。获得这些值后,我将其添加到列表中,并使用这两个列表准备带有值和键的映射。我想将此哈希映射添加到文件中。如果值已经存在,则无需添加 else add。但是我在将它添加到 Hashmap 并遍历 hashmap 以检查文件读取器读取行中是否存在 hashmap 键/值时遇到错误。

public class CompName {

    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub

        File file = new File("xml/input1.xml");
        ArrayList<String> email = new ArrayList<String>();
        ArrayList<String> comp = new ArrayList<String>();
        Map<ArrayList<String>,ArrayList<String>> compIdmap = new LinkedHashMap<ArrayList<String>,ArrayList<String>>();
        try {
            BufferedReader br = new BufferedReader(new FileReader(file));
            br.readLine();
            while(true){
                String line =br.readLine();
                //System.out.println("line "+line);
                if(line == null) break;
            if(line.contains("<CompanyName>"))
            {
                String compName = StringUtils.substringBetween(line, "<CompanyName>", "</CompanyName>");  //str =" middle "
                System.out.println(compName);
                comp.add(compName);
            }
            if(line.contains("<CorporateEmailAddress>"))
            {
                String emailId = StringUtils.substringBetween(line, "<CorporateEmailAddress>", "</CorporateEmailAddress>");  //str =" middle "
                if(emailId == null || emailId.equals(""))
                    emailId = "unknown";
                System.out.println(emailId);
                email.add(emailId);
            }

               for(int i=0;i<email.size();i++)
               {
                   compIdmap.put(email, comp);
               }
            }
            System.out.println("mapping :"+compIdmap);
BufferedWriter br1 = new BufferedWriter(new FileWriter("xml/mapping.txt"));
            Iterator it = compIdmap.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry pair = (Map.Entry)it.next();
                System.out.println(pair.getKey() + " = " + pair.getValue());
                br1.write(pair.getKey() + " = " + pair.getValue());
                it.remove(); // avoids a ConcurrentModificationException
            }

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }

}

输入xml文件来检查这些标签如下

    <?xml version="1.0" encoding="UTF-8"?>
<!-- Data provided by Bloomberg LP. -->
<FileDump>
<Version>IBXML 1.3</Version>
    <CompanyName>STANDARD CHARTERED B</CompanyName>
<EmailAddress>abc@gmail.com</EmailAddress>
<CorporateEmailAddress></CorporateEmailAddress>


<CompanyName>STANDARD CHARTERED B</CompanyName>
<EmailAddress>abc@gmail.com</EmailAddress>
<CorporateEmailAddress></CorporateEmailAddress>



<CompanyName>DBS BANK LIMITED HON</CompanyName>
<EmailAddress>nnn@bbg.com</EmailAddress>
<CorporateEmailAddress>nicholas@123.com</CorporateEmailAddress>

<CompanyName>DBS BANK LIMITED HON</CompanyName>
<EmailAddress>nnn@bbg.com</EmailAddress>
<CorporateEmailAddress>nicholas@123.com</CorporateEmailAddress>

<CompanyName>DBS BANK LIMITED HON</CompanyName>
<EmailAddress>nnn@bbg.com</EmailAddress>
<CorporateEmailAddress>nicholas@123.com</CorporateEmailAddress>

<CompanyName>DBS BANK (HONG KONG)</CompanyName>
<EmailAddress>www@bbg.com</EmailAddress>
<CorporateEmailAddress>WHEEL@123.com</CorporateEmailAddress>

<CompanyName>DBS BANK (HONG KONG)</CompanyName>
<EmailAddress>www@bbg.com</EmailAddress>
<CorporateEmailAddress>WHEEL@123.com</CorporateEmailAddress>
</FileDump>

我期待文件输出 mapping.txt 应该是

unknown STANDARD CHARTERED B
nicholas@123.com DBS BANK LIMITED HON
WHEEL@123.com DBS BANK (HONG KONG)
4

1 回答 1

0

代码有几个问题,第一个是您将映射的键和值定义为ArrayList. 键根本不能是数组列表 - 它没有逻辑意义,如果您希望值不同,请使用 Set。
注意:我理解它的方式是一封电子邮件属于一家公司,所以为什么不一对一映射呢?
为什么必须这样LinkedHashMap?你关心密钥插入的顺序吗?

这是一个有效的解决方案

public static void main(String[] args) throws IOException
{
    File file = new File("xml/input1.xml");

    // main data structure
    // key - corporate email
    // value - set of distinct companies
    // (does this make sense? a corporate email belongs to one company, no? 
    Map<String, Set<String>> compIdmap = new HashMap<String, Set<String>>();

    // making use of Java 7 try-with-resources to auto close the file after use 
    try (BufferedReader br = new BufferedReader(new FileReader(file))) {
        String line, compName = "", email = "";
        while ((line = br.readLine()) != null) {
            if (line.contains("<CompanyName>")) {
                compName = StringUtils.substringBetween(line, "<CompanyName>", "</CompanyName>");
            }
            if (line.contains("<CorporateEmailAddress>")) {
                email = StringUtils.substringBetween(line, "<CorporateEmailAddress>", "</CorporateEmailAddress>"); 
                if (email == null || email.equals("")) email = "unknown";
                Set<String> companiesSet = compIdmap.containsKey(email) ? compIdmap.get(email) : new HashSet<>();
                companiesSet.add(compName);
                compIdmap.put(email, companiesSet);
            }
        }
        System.out.println("mapping :" + compIdmap);
        BufferedWriter br1 = new BufferedWriter(new FileWriter("xml/mapping.txt"));
        Iterator it = compIdmap.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry pair = (Map.Entry) it.next();
            System.out.println(pair.getKey() + " = " + pair.getValue());
            br1.write(pair.getKey() + " = " + pair.getValue());
            it.remove(); // avoids a ConcurrentModificationException
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}

编辑:

这是一个产生确切所需输出的解决方案

public static void main(String[] args)
{
    File file = new File("xml/input1.xml");

    // contains email + " " + company 
    Set<String> emailAndCompanySet = new HashSet<>();

    // making use of Java 7 try-with-resources to auto close the file after use
    try (BufferedReader br = new BufferedReader(new FileReader(file))) {
        String line, compName = "", email = "";
        while ((line = br.readLine()) != null) {
            if (line.contains("<CompanyName>")) {
                compName = StringUtils.substringBetween(line, "<CompanyName>", "</CompanyName>");
            }
            if (line.contains("<CorporateEmailAddress>")) {
                email = StringUtils.substringBetween(line, "<CorporateEmailAddress>", "</CorporateEmailAddress>");
                if (email == null || email.equals(""))
                    email = "unknown";
                emailAndCompanySet.add(email + " " + compName);

            }
        }
        System.out.println("mapping :" + emailAndCompanySet);
        BufferedWriter br1 = new BufferedWriter(new FileWriter("xml/mapping.txt"));
        for (String emailAndCompany : emailAndCompanySet) {
            System.out.println(emailAndCompany);
            br1.write(emailAndCompany);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
于 2016-09-12T11:31:03.570 回答