简体   繁体   中英

Java: XML dom parsing retrieves just 1 element of the array

I have the following XML (provided by a web service)

<?xml version="1.0" encoding="UTF-8"?>
<itam>
   <status>OK</status>
   <data>
      <item0>
         <id>246</id>
         <prefisso_quadrato>1</prefisso_quadrato>
         <id_incontro_corrente />
         <id_giornata>65</id_giornata>
         <round>R1</round>
         <tempo>120</tempo>
         <punti_chong>0</punti_chong>
         <punti_hong>0</punti_hong>
         <amm_chong>0</amm_chong>
         <amm_hong>0</amm_hong>
      </item0>
      <item1>
         <id>247</id>
         <prefisso_quadrato>2</prefisso_quadrato>
         <id_incontro_corrente />
         <id_giornata>65</id_giornata>
         <round>R1</round>
         <tempo>120</tempo>
         <punti_chong>0</punti_chong>
         <punti_hong>0</punti_hong>
         <amm_chong>0</amm_chong>
         <amm_hong>0</amm_hong>
      </item1>
      <item2>
         <id>248</id>
         <prefisso_quadrato>3</prefisso_quadrato>
         <id_incontro_corrente />
         <id_giornata>65</id_giornata>
         <round>R1</round>
         <tempo>120</tempo>
         <punti_chong>0</punti_chong>
         <punti_hong>0</punti_hong>
         <amm_chong>0</amm_chong>
         <amm_hong>0</amm_hong>
      </item2>
   </data>
</itam>

I am trying to parse it in JAVA. I can access to the <status> and also to the <data> element. But when I try to iterate over <data> items, I can read just 1 element. This is the code:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
InputSource is = new InputSource(new StringReader(xml));
Document doc = builder.parse(is);
doc.getDocumentElement().normalize();
System.out.println(doc.getDocumentElement().getElementsByTagName("data").getLength());

OUTPUT: 1

My idea was something like the code below, but it runs just over the first element (I can read the rest element attributes and then it ends). How can I fix it? Thank you very much

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
InputSource is = new InputSource(new StringReader(xml));
Document doc = builder.parse(is);
doc.getDocumentElement().normalize();
NodeList nodelist = doc.getDocumentElement().getElementsByTagName("data");
if(nodelist!=null){
   for(int i=0; i<nodelist.getLength(); i++){
      Element el = (Element) nodelist.item(i);
      //use el to get data from it
   }
}

You will have to recurively iterate over the xml file to get all the elements.. something like this

private Document getDocument(String xsdUrl)
        throws ParserConfigurationException, SAXException, IOException {

    final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    final DocumentBuilder db = dbf.newDocumentBuilder();
    final Document doc = db.parse(xsdUrl);
    return doc;
}

private void processElementRecurse(final Element node) throws IOException,
ParserConfigurationException, SAXException, TransformerException {

    final NodeList nl = node.getChildNodes();

    for (int i = 0, n = nl.getLength(); i < n; i++) {
        final Node childNode = nl.item(i);

        if (childNode instanceof Element) {

            }

            else {
                processElementRecurse(childElement);
            }
        }

    }

The error is that you are looking for a list of <data> element and you have just one. A solution can be:

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    InputSource is = new InputSource(new StringReader(xml));
    Document doc = builder.parse(is);
    doc.getDocumentElement().normalize();
    NodeList items = doc.getDocumentElement().getElementsByTagName("data").item(0).getChildNodes();

    for(int i=0; i<items.getLength(); i++){
        System.out.println(items.item(i).getNodeName());
    }

Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM