使用XPATH表达式和Java解析XML文件
我想解析一个xml文件,我正在使用Java和xpath计算 我想用xpath表达式输出当前节点的同级,而不使用 使用getNextSibling()函数,可能吗 e、 g.如果我们读取name元素,我想添加xpath表达式=“./address” 为了在不使用getNextSibling()的情况下输出“name”的同级 xml文件如下所示:使用XPATH表达式和Java解析XML文件,java,xml,parsing,dom,Java,Xml,Parsing,Dom,我想解析一个xml文件,我正在使用Java和xpath计算 我想用xpath表达式输出当前节点的同级,而不使用 使用getNextSibling()函数,可能吗 e、 g.如果我们读取name元素,我想添加xpath表达式=“./address” 为了在不使用getNextSibling()的情况下输出“name”的同级 xml文件如下所示: <root> <name> <address>
<root>
<name>
<address>
<profession>
</root>
package dom_stack4;
import org.w3c.dom.*;
import javax.xml.xpath.*;
import javax.xml.parsers.*;
import java.io.IOException;
import org.xml.sax.SAXException;
public class Dom_stack4 {
public static void main(String[] args) throws ParserConfigurationException, SAXException,
IOException, XPathExpressionException {
// TODO code application logic here
DocumentBuilderFactory domFactory =
DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true);
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document doc = builder.parse("root.xml");
XPath xpath = XPathFactory.newInstance().newXPath();
// XPath Query for showing all nodes value
XPathExpression expr = xpath.compile("/root/name/text()");
Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println(" Name is : " + nodes.item(i).getNodeValue());
/* IS that possible here ?? */
/* ./address/text() => outputs the current's node sibling */
expr = xpath.compile("./address/text()");
Object result1 = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nodes1 = (NodeList) result1;
for (int j = 0; j < nodes1.getLength(); j++) {
System.out.println(" ---------------- " + nodes1.item(j).getNodeValue());
}
}
}
}
我的代码如下:
<root>
<name>
<address>
<profession>
</root>
package dom_stack4;
import org.w3c.dom.*;
import javax.xml.xpath.*;
import javax.xml.parsers.*;
import java.io.IOException;
import org.xml.sax.SAXException;
public class Dom_stack4 {
public static void main(String[] args) throws ParserConfigurationException, SAXException,
IOException, XPathExpressionException {
// TODO code application logic here
DocumentBuilderFactory domFactory =
DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true);
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document doc = builder.parse("root.xml");
XPath xpath = XPathFactory.newInstance().newXPath();
// XPath Query for showing all nodes value
XPathExpression expr = xpath.compile("/root/name/text()");
Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println(" Name is : " + nodes.item(i).getNodeValue());
/* IS that possible here ?? */
/* ./address/text() => outputs the current's node sibling */
expr = xpath.compile("./address/text()");
Object result1 = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nodes1 = (NodeList) result1;
for (int j = 0; j < nodes1.getLength(); j++) {
System.out.println(" ---------------- " + nodes1.item(j).getNodeValue());
}
}
}
}
package dom_stack4;
导入org.w3c.dom.*;
导入javax.xml.xpath.*;
导入javax.xml.parsers.*;
导入java.io.IOException;
导入org.xml.sax.SAXException;
公共类Dom_stack4{
公共静态void main(字符串[]args)抛出ParserConfiguration异常、SAXException、,
IOException,XPathExpressionException{
//此处的TODO代码应用程序逻辑
DocumentBuilderFactory domFactory=
DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true);
DocumentBuilder=domFactory.newDocumentBuilder();
documentdoc=builder.parse(“root.xml”);
XPath=XPathFactory.newInstance().newXPath();
//用于显示所有节点值的XPath查询
XPathExpression expr=xpath.compile(“/root/name/text()”);
Object result=expr.evaluate(doc,XPathConstants.NODESET);
节点列表节点=(节点列表)结果;
对于(int i=0;i输出当前节点的同级节点*/
expr=xpath.compile(“./address/text()”);
Object result1=expr.evaluate(doc,XPathConstants.NODESET);
节点列表nodes1=(节点列表)结果1;
对于(int j=0;j
谢谢,请提前首先,如果您正在使用格式正确的xml运行代码,则最好关闭名称地址和职业标记,否则在尝试解析它时会出现错误。其次,如果要选择节点的文本子级,则需要确保确实存在某些内容,因此xml应该如下所示:
<root>
<name>hi</name>
<address>hi</address>
<profession>hi</profession>
</root>
接下来,您应该确保XPath实际上是正确的。我们当前选择的节点是name的文本节点。这意味着我们需要先转到name节点,而不是text节点。XPath语法中的
表达式选择当前节点,因此您所要做的就是选择相同的节点。您需要选择父节点的。
表达式
因此,使用当前的XPath。
我们正在选择名称节点。我们要做的是选择属于名称节点的地址节点。有两种方法可以做到这一点,我们可以选择名称节点的父节点,根节点,并选择作为其子节点的地址节点,或者我们可以使用XPath轴选择同级节点(有关轴的信息可以在此处找到)
如果我们要遍历根节点,我们需要选择当前节点的父节点的父节点,因此,。/..
将为我们提供根节点,然后是地址子节点:。/../address/text()
,将为我们提供所有地址同级
或者,使用轴,我们可以执行。
选择名称节点,后跟。/以下同级::address
(注意:这仅在地址节点位于名称节点之后时有效),然后使用选择地址节点的文本。/以下同级:address/text()
这给了我们这两条线
expr = xpath.compile("../../address/text()");
Object result1 = expr.evaluate(nodes.item(i), XPathConstants.NODESET);
或