Obtaining non-XML nodes via the recursion method (in Java)

advertisements

I'm trying to read out the topics of a XML-file (out of Help & Manual) one by one by using a recursion method.

Unfortunately there seems to be some mistake as the programm doesn't print out the topics like I want to (it prints out nothing) and gets stuck in stackoverflow. I don't find it though..

I'd appreciate any help!

EDIT: I spent some time in Debugging, one problem besides the recursion itself is definitely that it reads out the wrong "nodes" for topicref (it's attributes). I don't know how to make it read out caption though.. + it doesn't run analogous as it only creates 1 topicref.

Code:

        package org.joox;

        import java.io.IOException;

        import javax.xml.parsers.DocumentBuilder;
        import javax.xml.parsers.DocumentBuilderFactory;
        import javax.xml.parsers.ParserConfigurationException;

        import org.w3c.dom.Document;
        import org.w3c.dom.Node;
        import org.w3c.dom.NodeList;
        import org.xml.sax.SAXException;

        public class XPathDemo {
        DocumentBuilderFactory factory =
        DocumentBuilderFactory.newInstance();
        DocumentBuilder builder;
        Document doc = null;

    public static void main(String[] args) {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        DocumentBuilder builder;
        Document doc = null;
        try {
            builder = factory.newDocumentBuilder();
            doc = builder.parse("table_of_contents.xml");

            GiveCaption(doc.getChildNodes().item(0));
        } catch (ParserConfigurationException | SAXException | IOException e) {
            e.printStackTrace();
        }

    }

    private static void GiveCaption(Node n) {
        // no child nodes + no caption -> end
        if (!n.hasChildNodes() && !n.getNodeName().equals("caption")) {
            return;
        }
        NodeList nodes = n.getChildNodes();

        if (n.getChildNodes().getLength() == 0)

        {
            return;
        }
        if (n.getNodeName().equals("topicref")) {
            if (n.getNodeName().equals("caption")) {
                System.out.println(n);
            }
        }

         for (int i = 0; i < nodes.getLength(); i++) {
         n = nodes.item(i);
         }
        for (int i = 0; i < nodes.getLength(); i++) {
            GiveCaption(n);
        }

    }
}

XML:

<?xml version="1.0" encoding="UTF-8"?>
<map xmlns:xsi="http://www.w3.org/2001/XInclude">
  <topicref type="topic" id="429303521027079" build="ALL" modified="2017-07-14T10:50:14.916Z" icon="0" href="Introduction">
    <caption translate="true">Introduction</caption>
    <topicref type="topic" id="429305694503733" build="ALL" modified="2017-07-14T10:50:15.258Z" icon="0" href="Welcome-topic">
      <caption translate="true">Welcome topic</caption>
    </topicref>
    <topicref type="topic" id="42930890558253" build="ALL" modified="2017-07-14T10:50:15.479Z" icon="0" href="Second-topic">
      <caption translate="true">Second topic</caption>
    </topicref>
  </topicref>
  <topicref type="topic" id="429303877549160" build="ALL" modified="2017-07-14T10:50:15.711Z" icon="0" href="Chapter-2">
    <caption translate="true">Chapter 2</caption>
    <topicref type="topic" id="429304160418503" build="ALL" modified="2017-07-14T10:50:15.937Z" icon="0" href="Overview">
      <caption translate="true">Overview</caption>
    </topicref>
    <topicref type="topic" id="429304436298052" build="ALL" modified="2017-07-14T10:50:16.168Z" icon="0" href="Sub-chapter-2_1">
      <caption translate="true">Sub chapter 2.1</caption>
      <topicref type="topic" id="429302637318652" build="ALL" modified="2017-07-14T10:50:16.395Z" icon="0" href="New-topic">
        <caption translate="true">New topic</caption>
      </topicref>
    </topicref>
  </topicref>
</map>


I know this doesn't answer your question directly, but you could save yourself a lot of trouble and get started with JAXB right away. http://www.vogella.com/tutorials/JAXB/article.html

JAXB is a Java standard that defines how Java objects are converted from and to XML. It uses a standard set of mappings.

The only reason when JAXB may not be suitable is if you are reading very large XML files, and you would like to stream the content and read+process on the fly.

Even in that case, STAX (the newer Java XML library) is easier to use than SAX. http://www.vogella.com/tutorials/JavaXML/article.html#javastax