Java How to extract a complete XML block

倖福魔咒の 提交于 2019-11-27 04:06:29

问题


Using this XML example:

<A>
  <B>
    <id>0</id>
  </B>
  <B>
    <id>1</id>
  </B>
</A>

I want a simple method to extract the XML block of node B, returning the XML String:

<B>
  <id>1</id>
</B>

To retrieve this node i should use some Java XPath library like XOM or Java XPath, but i couldn't find how to get the complete XML string.

I found two equivalent answered questions using C#: C# How to extract complete xml node set and how can I extract an XML block from an XML document?


回答1:


Adding to lwburk's solution, to convert a DOM Node to string form, you can use a Transformer:

private static String nodeToString(Node node)
throws TransformerException
{
    StringWriter buf = new StringWriter();
    Transformer xform = TransformerFactory.newInstance().newTransformer();
    xform.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    xform.transform(new DOMSource(node), new StreamResult(buf));
    return(buf.toString());
}

Complete example:

public static void main(String... args)
throws Exception
{
    String xml = "<A><B><id>0</id></B><B><id>1</id></B></A>";
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    Document doc = dbf.newDocumentBuilder().parse(new InputSource(new StringReader(xml)));

    XPath xPath = XPathFactory.newInstance().newXPath();
    Node result = (Node)xPath.evaluate("A/B[id = '1']", doc, XPathConstants.NODE);

    System.out.println(nodeToString(result));
}

private static String nodeToString(Node node)
throws TransformerException
{
    StringWriter buf = new StringWriter();
    Transformer xform = TransformerFactory.newInstance().newTransformer();
    xform.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    xform.transform(new DOMSource(node), new StreamResult(buf));
    return(buf.toString());
}



回答2:


The expression needed to refer to that second B element should look something like this:

/*/B[id='1']

Or, if the target node is at an unknown position in the document, use:

//B[id='1']

Full Java example (assuming the XML is in a file called workbook.xml):

DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document doc = builder.parse("workbook.xml");
XPath xpath = XPathFactory.newInstance().newXPath();
XPathExpression expr = xpath.compile("//B[id='1']");        

NodeList nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
for (int i = 0; i < nodes.getLength(); i++) {
    System.out.println("[" + nodes.item(i) + "]");
}


来源:https://stackoverflow.com/questions/8979851/java-how-to-extract-a-complete-xml-block

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!