Java XPath API - 获取表示子树的字符串 [英] Java XPath API - get string representing subtree

查看:232
本文介绍了Java XPath API - 获取表示子树的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题不是关于xpath语法,而是与围绕xpath的java API有关。请考虑以下xml:

My question is not about xpath syntax, it's related to the java API surrounding xpath. Consider the following xml:

<wrapper>
    <metadata>
        <somefield>somevalue</somefield>
        <anotherfield>othervalue</anotherfield>
    </metadata>
    <data>
        <some>
            <unique>
                <xml>
                    <structure>stuff</structure>
                </xml>
            </unique>
        </some>
    </data>
</wrapper>

我可以使用以下代码轻松获取使用xpath的元数据字段:

I can easily get the metadata fields using xpath by using the following code:

 XPath xp = XPathFactory.newInstance().newXPath();
 Node node = (Node) xp.evaluate("/wrapper/metadata/somefield", xmlDoc, XPathConstants.NODE);
 String somefield = node.getFirstChild().getNodeValue();

我正在努力学习如何获得代表子树的字符串xml以< some> 标记开头。换句话说,我编写什么代码来获取一个字符串,打印出来时会打印出以下内容? xpath查询将是/ wrapper / data / some,但我不知道如何正确使用xpath api。

I am struggling with how I can get a string representing the subtree of xml starting with the <some> tag. In other words, what code do I write to get a String that when printed out, would print out the following? The xpath query would be "/wrapper/data/some", but I do not know how to utilize the xpath api appropriately.

<some>
    <unique>
        <xml>
            <structure>stuff</structure>
        </xml>
    </unique>
</some>


推荐答案

您只需转换节点您使用<$ c $从 XPathExpression 返回到 String c> Transformer 就像你将文档写入文件一样,这是一个完整的例子:

You simply need to transform the Node you get back from the XPathExpression to a String using a Transformer as you would if you were writing the document to a file, here is a full example:

public static void main(String[] args) throws Exception {
    final String xml = "<wrapper>\n"
            + "    <metadata>\n"
            + "        <somefield>somevalue</somefield>\n"
            + "        <anotherfield>othervalue</anotherfield>\n"
            + "    </metadata>\n"
            + "    <data>\n"
            + "        <some>\n"
            + "            <unique>\n"
            + "                <xml>\n"
            + "                    <structure>stuff</structure>\n"
            + "                </xml>\n"
            + "            </unique>\n"
            + "        </some>\n"
            + "    </data>\n"
            + "</wrapper>";
    final Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(xml.getBytes(Charsets.UTF_8)));
    final XPathExpression xpath = XPathFactory.newInstance().newXPath().compile("//some");
    final Node node = (Node) xpath.evaluate(doc, XPathConstants.NODE);
    StringWriter sw = new StringWriter();
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
    transformer.transform(new DOMSource(node), new StreamResult(sw));
    System.out.println(sw.toString());
}

这篇关于Java XPath API - 获取表示子树的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆