簡體   English   中英

Java 解析 XML 父子元素同名

[英]Java parse XML parent and child elements with the same name

我正在嘗試解析一些服務器設置。

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

我正在嘗試將兩個監聽端口解析為我的 Java 程序中的變量,但是當我還想解析另一個端口時,我似乎只得到了 SSL 端口。

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

當我嘗試在上面的示例中顯示 HTTP 端口(第二個監聽端口 XML 標記,但在父標記中)時,它顯示 SSL 端口。 System.out.println("Https:" + HttpsPort); 似乎可以正常工作。

任何人的想法?

您應該使用 XPath 來提取正確的節點,如下所示:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM