嗨,我正在解析 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)