使用XPathJava读取标记内部
Hye我不熟悉使用Java读取XML文件。我的问题是,我一直在尝试读取XML,在一个特定的标记之间,我想获取所需的数据。我正在使用XPath,我的查询是:使用XPathJava读取标记内部,java,xml,xpath,Java,Xml,Xpath,Hye我不熟悉使用Java读取XML文件。我的问题是,我一直在尝试读取XML,在一个特定的标记之间,我想获取所需的数据。我正在使用XPath,我的查询是: String expression = "/ADOXML/MODELS/MODEL/MODELATTRIBUTES/ATTRIBUTE[@type='STRING']"; 它工作正常,我要读取的特定标签是: <ATTRIBUTE name="Description" type="STRING"> SOME TEXT
String expression = "/ADOXML/MODELS/MODEL/MODELATTRIBUTES/ATTRIBUTE[@type='STRING']";
它工作正常,我要读取的特定标签是:
<ATTRIBUTE name="Description" type="STRING"> SOME TEXT </ATTRIBUTE>
在标签里面!
有人能帮我怎么做吗?我对xml阅读还不熟悉!尽我所能:
String expression = "/ADOXML/MODELS/MODEL/MODELATTRIBUTES/ATTRIBUTE[@name='Description' and ./type/text()='STRING']";
但它不会给我任何输出!
提前谢谢
我的代码:
DocumentBuilderFactory builderFactory =
DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
try {
builder = builderFactory.newDocumentBuilder();
org.w3c.dom.Document document = builder.parse(
new FileInputStream("c:\\y.xml"));
XPath xPath = XPathFactory.newInstance().newXPath();
String expression = "/ADOXML/MODELS/MODEL/MODELATTRIBUTES/ATTRIBUTE[@name='Description'and @type='STRING']";
System.out.println(expression);
NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(document, XPathConstants.NODESET);
for (int i = 0; i < nodeList.getLength(); i++) {
System.out.println(nodeList.item(i).getFirstChild().getNodeValue());
}
} catch (ParserConfigurationException | SAXException | IOException e) {
System.out.print(e);
}
DocumentBuilderFactory builderFactory=
DocumentBuilderFactory.newInstance();
DocumentBuilder=null;
试一试{
builder=builderFactory.newDocumentBuilder();
org.w3c.dom.Document Document=builder.parse(
新文件输入流(“c:\\y.xml”);
XPath=XPathFactory.newInstance().newXPath();
字符串表达式=“/ADOXML/MODELS/MODEL/modeldattributes/ATTRIBUTE[@name='Description'和@type='String']”;
System.out.println(表达式);
NodeList NodeList=(NodeList)xPath.compile(expression.evaluate(document,XPathConstants.NODESET);
for(int i=0;i
我的代码有问题,我搞不懂是什么 这段代码对我来说很好,XPath更改为:
“/ADOXML/MODELS/MODEL/modeldattributes/ATTRIBUTE[@name='Description'][@type='STRING']”
:
上面提到的代码运行良好 您也可以尝试其他方法来获取文本节点-
String expression = "/ADOXML/MODELS/MODEL/MODELATTRIBUTES/ATTRIBUTE/text()";
NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(document, XPathConstants.NODESET);
System.out.println(nodeList.item(0).getNodeValue());
lols你怎么能说,即使你是在阅读了问题后发布的,我也必须阅读一个xml文件,然后才想获取标签库中的数据,但我不理解你的评论。我没有您使用的XML,所以我使用了内存中的示例和您的代码。有了这个例子,一切都正常了。因此,关于您在其中读取的XML文档,您的XPath可能不正确。如果您也喜欢发布XML文档。如果我遗漏了什么,很抱歉。我正在提供XML、XML或XSD或您希望匹配XPath的XML片段。我已经发布了我的XML部分不知道您的意思
private static final String EXAMPLE_XML =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<ADOXML adoversion=\"Version 5.1\" username=\"kvarga\" database=\"adonisdb\" time=\"08:55\" date=\"30.11.2013\" version=\"3.1\">" +
"<MODELS>" +
"<MODEL version=\"\" applib=\"ADONIS BPMS BP Library 5.1\" libtype=\"bp\" modeltype=\"Business process model\" name=\"Product development\" id=\"mod.25602\">" +
"<MODELATTRIBUTES>" +
"<ATTRIBUTE name=\"Version number\" type=\"STRING\"> </ATTRIBUTE>" +
"<ATTRIBUTE name=\"Author\" type=\"STRING\">kvarga</ATTRIBUTE>" +
"<ATTRIBUTE name=\"Description\" type=\"STRING\">I WANT THIS PARA 2</ATTRIBUTE>" +
"</MODELATTRIBUTES>" +
"</MODEL>" +
"</MODELS>" +
"</ADOXML>";
public static void main(String[] args) {
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
try {
builder = builderFactory.newDocumentBuilder();
Document document = builder.parse(new ByteArrayInputStream(EXAMPLE_XML.getBytes()));
XPath xPath = XPathFactory.newInstance().newXPath();
String expression = "/ADOXML/MODELS/MODEL/MODELATTRIBUTES/ATTRIBUTE[@name='Description'][@type='STRING']";
NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(document, XPathConstants.NODESET);
for (int i = 0; i < nodeList.getLength(); i++) {
System.out.println("###" + nodeList.item(i).getFirstChild().getNodeValue() + "###");
}
} catch (Exception e) {
System.out.print(e);
}
}
###I WANT THIS PARA 2###
String expression = "/ADOXML/MODELS/MODEL/MODELATTRIBUTES/ATTRIBUTE/text()";
NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(document, XPathConstants.NODESET);
System.out.println(nodeList.item(0).getNodeValue());