gpt4 book ai didi

java - XPathFactory返回xml文档

转载 作者:行者123 更新时间:2023-12-01 12:27:26 25 4
gpt4 key购买 nike

我想通过 XPathFactory 列出 xml 文档中的一些节点,但我得到的是没有 xml 节点的纯文本作为返回我的Xml文档是这样的:

<?xml version="1.0" encoding="utf-8"?>
<journal>
<title>Hakim Research Journal</title>
<subject>Medical Sciences</subject>

<articleset>
<article>
<title>Challenges and Performance Improvement Approaches of Boards of Trustees of Universities of Medical Sciences and Health Services </title>
<content_type>Original</content_type>
</article>
<article>

<title> Risk Factors of Infant Mortality Rate in North East of Iran </title>
<content_type>Original</content_type>
</article>

</articleset>
</journal>

我想要返回元素,如下所示:

    <article>
<title>Challenges and Performance Improvement Approaches of Boards of Trustees of Universities of Medical Sciences and Health Services in Iran </title>
<content_type>Original</content_type>
</article>
<article>
<title> Risk Factors of Infant Mortality Rate in North East</title>
<content_type>Original</content_type>
</article>

我的代码实现是:

String result="";
File xmlDocument = new File("e://journal_last.xml");
InputSource inputSource = new InputSource(new FileInputStream(xmlDocument));
XPathFactory factory=XPathFactory.newInstance();
XPath xPath=factory.newXPath();
XPathExpression articleset;
articleset = xPath.compile("/journal/articleset/)");
result = articleset.evaluate(inputSource);
System.out.println(result);

但它只返回没有节点标签的节点的纯文本!输出为:

Challenges and Performance Improvement Approaches of Boards of Trustees of Universities of     Medical Sciences and Health Services  
Original



Risk Factors of Infant Mortality Rate in North East
Original

你能帮我一下吗?我感谢任何帮助。

最佳答案

您可以在 Document 上评估 XPath 以获取文章的 NodeList,然后编写 NodeList > 作为 xml。

public class Main {

public static void main(String[] args) throws Exception {
File xmlDocument = new File("e://journal_last.xml");
FileInputStream xmlInputStream = new FileInputStream(xmlDocument);

Document doc = parseDocument(new InputSource(xmlInputStream));
NodeList articleList = evaluateXPath(doc, "/journal/articleset",
NodeList.class);
String xmlString = writeXmlString(articleList);

System.out.println(xmlString);

}

private static Document parseDocument(InputSource inputSource)
throws ParserConfigurationException, SAXException, IOException {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder docBuilder = documentBuilderFactory
.newDocumentBuilder();
Document doc = docBuilder.parse(inputSource);
return doc;
}

@SuppressWarnings("unchecked")
private static <T> T evaluateXPath(Document doc, String xpath,
Class<T> resultType) throws XPathExpressionException {
QName returnType = resolveReturnType(resultType);

XPathFactory factory = XPathFactory.newInstance();
XPath xPath = factory.newXPath();
XPathExpression xpathExpression = xPath.compile(xpath);

Object resultObject = xpathExpression.evaluate(doc, returnType);
return (T) resultObject;
}

private static QName resolveReturnType(Class<?> clazz) {
if (NodeList.class.equals(clazz)) {
return XPathConstants.NODESET;
} else {
throw new UnsupportedOperationException("Not implemented yet");
}
}

private static String writeXmlString(NodeList nodeList)
throws TransformerConfigurationException,
TransformerFactoryConfigurationError, TransformerException {

StreamResult streamResult = new StreamResult(new StringWriter());
TransformerFactory transformerFactory = TransformerFactory
.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");

for (int i = 0; i < nodeList.getLength(); i++) {
Node articleListItem = nodeList.item(i);
DOMSource source = new DOMSource(articleListItem);
transformer.transform(source, streamResult);
}

String xmlString = streamResult.getWriter().toString();
return xmlString;
}
}

关于java - XPathFactory返回xml文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26204684/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com