Search code examples
javaxml-parsingxmldom

Java parse XML parent and child elements with the same name


I'm trying to parse some server settings.

  <server>
    <name>HTTP server</name>
    <ssl>
      <name>HTTPS server</name>
      <listen-port>8051</listen-port>
    </ssl>
    <listen-port>8050</listen-port>
  </server>

I'm trying to parse both listen ports to variables in my Java program, but I only seem to get the SSL port when I also want to parse the other port.

            File inputFile = new File(String.valueOf(Paths.get(serverPath, "config", "config.xml")));
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(inputFile);
            doc.getDocumentElement().normalize();

            NodeList nList = doc.getElementsByTagName("server");

            for (int temp = 0; temp < nList.getLength(); temp++) {
                Node node = nList.item(temp);

                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    Element eElement = (Element) node;

                    try {
                        HttpPort = eElement.getElementsByTagName("listen-port").item(0).getTextContent();
                        System.out.println("Http: " + HttpPort);
                        Node sslSettings = eElement.getElementsByTagName("ssl").item(0);

                        if (nList.getLength() == 1) {
                            Element sslElement = (Element) sslSettings;
                            System.out.println(sslElement);
                            HttpsPort = sslElement.getElementsByTagName("listen-port").item(0).getTextContent();
                            System.out.println("Https: " + HttpsPort);
                        }

When I try to display the HTTP port in above example (the second listen-port XML tag, but in the parent), it shows the SSL port. System.out.println("Https: " + HttpsPort); Seems to work as it should.

Anyone an idea?


Solution

  • You should use XPath to extract the right node like so:

    import org.w3c.dom.Document;
    import org.xml.sax.InputSource;
    import org.xml.sax.SAXException;
    
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.ParserConfigurationException;
    import javax.xml.xpath.XPath;
    import javax.xml.xpath.XPathConstants;
    import javax.xml.xpath.XPathExpressionException;
    import javax.xml.xpath.XPathFactory;
    import java.io.IOException;
    import java.io.StringReader;
    
    public class FindViaXpath {
    
      private static final String XML =
        " <server>\n" +
          "    <name>HTTP server</name>\n" +
          "    <ssl>\n" +
          "      <name>HTTPS server</name>\n" +
          "      <listen-port>8051</listen-port>\n" +
          "    </ssl>\n" +
          "    <listen-port>8050</listen-port>\n" +
          "  </server>";
    
      public static void main(String[] args) {
        System.out.println(XML);
    
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = null;
        try {
          builder = factory.newDocumentBuilder();
          StringReader reader = new StringReader(XML);
          InputSource source = new InputSource(reader);
          Document document = builder.parse(source);
          XPath xpath = XPathFactory.newInstance().newXPath();
          String port = (String) xpath.evaluate("//server/listen-port", document, XPathConstants.STRING);
          System.out.println(String.format("Port: %s", port));
        } catch (ParserConfigurationException | SAXException | IOException | XPathExpressionException e) {
          e.printStackTrace();
        }
      }
    }