XML : XML parser skipping duplicate values JAVA

I've written an XML parser which parses an XML documents and returns me a Map of and ID and Name. For some reason it's skipping duplicates IDs.

Example

  <NameDefinition>       <ID>/Return/ReturnData/IRS1040/TaxAmt</ID>       <Name>the amount of tax you owe</Name>  </NameDefinition>  <NameDefinition>       <ID>/Return/ReturnData/IRS1040/TaxAmt</ID>       <Name>the amount of tax youleskrnve </Name>  </NameDefinition>  <NameDefinition>      <ID>/Return/ReturnData/IRS1040/WagesSalariesAndTipsWorksheet/TotalWagesSalariesAndTipsGrpPP/TotalEarnedIncomePP</ID>      <Name>total earned income</Name>  </NameDefinition>    

I should get a list of IDs like this:

  • /Return/ReturnData/IRS1040/TaxAmt
  • /Return/ReturnData/IRS1040/TaxAmt
  • /Return/ReturnData/IRS1040/WagesSalariesAndTipsWorksheet/TotalWagesSalariesAndTipsGrpPP/TotalEarnedIncomePP

But I am getting

  • /Return/ReturnData/IRS1040/TaxAmt
  • /Return/ReturnData/IRS1040/WagesSalariesAndTipsWorksheet/TotalWagesSalariesAndTipsGrpPP/TotalEarnedIncomePP

For some reason it's ignoring the duplicate XML:

  <NameDefinition>       <ID>/Return/ReturnData/IRS1040/TaxAmt</ID>       <Name>the amount of tax youleskrnve </Name>  </NameDefinition>    

Here is my code:

  public static Map<String,String> getMap(String pathToFile) {        Map<String,String> map = new LinkedHashMap<String, String>();        try {            Document doc = getDocument(pathToFile);          NodeList nList = doc.getElementsByTagName("NameDefinition");            for(int i=0;i<nList.getLength();i++) {              Node nNode = nList.item(i);              if(nNode.getNodeType() == Node.ELEMENT_NODE) {                  Element eElement = (Element) nNode;                  String nodeID = eElement.getElementsByTagName("ID").item(0).getTextContent();                   NodeList n = eElement.getElementsByTagName("Name");                    for(int j=0;j<n.getLength();j++) {                      String name = n.item(0).getTextContent();                        if(name.equalsIgnoreCase("")) {                          name = "blank"; // check for empty values                      }                      map.put(nodeID, name);                  }              }          }      }       catch (IOException e) {          e.printStackTrace();      }      return map;  }      public static List<String> getIDList(String pathToFile) {      List<String> list = new ArrayList<String>();        Map<String, String> map = getMap(pathToFile);        for(String id : map.keySet()) {          list.add(id);      }      return list;  }    

My question is, why is this happening? Why duplicate is being ignored?

No comments:

Post a Comment