我正在通过StackOverflow寻找,但我找不到最佳答案。我需要在JAVA中读取XML文件。我的XML文件如下所示:`
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<server>
<server01>
<department>A1</department>
<department>A2</department>
</server01>
<server02>
<department>A1</department>
<department>A2</department>
</server02>
</server>
是否有可能在<server01>
和<server02>
之间读取Java元素,不包括文件的其余部分?或者我需要使用不同的标签?我找到了如何读取父节点的方法:
NodeList serversNames = xmlD.getDocumentElement().getChildNodes();
for (int i = 0; i < serversNames.getLength(); i++) {
Node node = serversNames.item(i);
if (node instanceof Element) {
Element childElement = (Element) node;
System.out.println("tag name: " + childElement.getTagName());
}
}
我能够阅读所有部门标签并将其保存到阵列:
NodeList serverName = xmlD.getElementsByTagName("department");
serversList = new String[serverName.getLength()];
System.out.println("zasieg: " + serverName.getLength());
for (int temp = 0; temp < serverName.getLength(); temp++) {
Element shareName = null;
shareName = (Element) serverName.item(temp);
serversList[temp] = shareName.getTextContent();
System.out.println(temp + " - " + serversList[temp]);
}
那么,是否有可能只读取父节点的元素?只有SERVER01的元素?
答案 0 :(得分:3)
XPath就是你想要的。对于所有意图和目的,您可以像sql一样考虑xpath,只考虑xml文档而不是数据库。这是一个使用Java的简单示例(请记住,xpath是一个标准,并不是特定于java,因此您可以在几乎任何流行语言中找到许多方法):
// Load document
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse( new FileInputStream( "/tmp/xml" ) );
// Create XPath expression
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression expr = xpath.compile( "//server01" );
// Find node 'server01'
Node node = (Node) expr.evaluate( doc, XPathConstants.NODE );
if( node == null ) {
System.out.println( "Node not found" );
System.exit( 0 );
}
// Extract departments
Element server01 = (Element) node;
for( int k = 0 ; k < server01.getChildNodes().getLength() ; k++ ) {
Node childNode = server01.getChildNodes().item( k );
// Check if current node is a department node
if( "department".equals( childNode.getNodeName() ) ) {
System.out.println( childNode.getNodeName() + ": " + childNode.getTextContent().trim() );
}
}
是否有可能避免这种情况?
是的,只需将XPath表达式更改为仅接收所需的节点。
// Load document
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse( new FileInputStream( "/tmp/xml" ) );
// Create XPath expression
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression expr = xpath.compile( "//server01/department" );
// Find nodes 'department' under node 'server01'
NodeList node = (NodeList) expr.evaluate( doc, XPathConstants.NODESET );
// Extract departments
for( int k = 0 ; k < node.getLength() ; k++ ) {
Node childNode = node.item( k );
// Check if current node is a department node
if( "department".equals( childNode.getNodeName() ) ) {
System.out.println( "[" + k + "] " + childNode.getNodeName() + ": " + childNode.getTextContent().trim() );
}
}
您必须收到下一个输出:
[0] department: A1
[1] department: A2