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();
}
}
}
我正在尝试解析一些服务器设置。
<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();
}
}
}