0

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
   }
}
3
  • 2
    There is only one <data> in the file. So Element el is the first and single <data> node. You should get the items by el.getChildNodes() and iterate through that list. Commented Sep 26, 2016 at 13:16
  • Seems like you intend to read the children of <data> Commented Sep 26, 2016 at 13:20
  • Only the children that are Element. Commented Sep 26, 2016 at 13:24

2 Answers 2

1

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!

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much! I didn't look at that :)
0

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);
            }
        }

    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.