Java XPathFactory返回xml文档

Java XPathFactory返回xml文档,java,xpath,Java,Xpath,我想通过XPathFactory列出xml文档中的一些节点,但我得到的是纯文本,没有xml节点作为返回 我的Xml文档如下所示: <?xml version="1.0" encoding="utf-8"?> <journal> <title>Hakim Research Journal</title> <subject>Medical Sciences</subject> <articleset> <ar

我想通过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>
但是它只返回没有节点标记的节点的纯文本! 输出为:

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
你能帮帮我吗?
非常感谢您的帮助。

您可以在
文档中评估
XPath
,以获取文章的
节点列表
,然后将
节点列表
写成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;
    }
}
公共类主{
公共静态void main(字符串[]args)引发异常{
文件xmlDocument=新文件(“e://journal_last.xml”);
FileInputStream xmlInputStream=新的FileInputStream(xmlDocument);
Document doc=parseDocument(新的InputSource(xmlInputStream));
NodeList articleList=evaluateXPath(doc,“/journal/articleset”,
节点列表(类);
String xmlString=writeXmlString(articleList);
System.out.println(xmlString);
}
私有静态文档parseDocument(InputSource InputSource)
抛出ParserConfiguration异常、SAXException、IOException{
DocumentBuilderFactory DocumentBuilderFactory=DocumentBuilderFactory
.newInstance();
DocumentBuilder docBuilder=documentBuilderFactory
.newDocumentBuilder();
Document doc=docBuilder.parse(inputSource);
退货单;
}
@抑制警告(“未选中”)
私有静态路径(文档文档、字符串xpath、,
类resultType)抛出XPathExpressionException{
QName returnType=resolveReturnType(resultType);
XPathFactory=XPathFactory.newInstance();
XPath=factory.newXPath();
XPathExpression=xPath.compile(xPath);
objectresultobject=xpathExpression.evaluate(doc,returnType);
返回(T)结果对象;
}
私有静态QName resolveReturnType(类clazz){
if(NodeList.class.equals(clazz)){
返回XPathConstants.NODESET;
}否则{
抛出新的UnsupportedOperationException(“尚未实现”);
}
}
私有静态字符串writeXmlString(NodeList NodeList)
引发TransformerConfiguration异常,
TransformerFactoryConfigurationError,TransformerException{
StreamResult StreamResult=新的StreamResult(新的StringWriter());
TransformerFactory TransformerFactory=TransformerFactory
.newInstance();
Transformer Transformer=transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT,“是”);
for(int i=0;i
它可以是编译字符串(
“/journal/articleset/)”
)中的额外括号吗?只是想一想……谢谢,我已经纠正了海报的XML示例不使用名称空间,但许多XML文档使用名称空间,XPath是在带有名称空间的XML上定义的,因此我认为在
DocumentBuilderFactory
上使用它更好更安全,例如
DocumentBuilderFactory.setNamespaceAware(true)
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;
    }
}