简体   繁体   中英

How to use java to get element of xml document, but in xml string format?

I have read some links on parsing xml document like below:

<inventory>
    <book year="2000">
        <title>Snow Crash</title>
        <author>Neal Stephenson</author>
        <publisher>Spectra</publisher>
        <isbn>0553380958</isbn>
        <price>14.95</price>
    </book>

    <book year="2005">
        <title>Burning Tower</title>
        <author>Larry Niven</author>
        <author>Jerry Pournelle</author>
        <publisher>Pocket</publisher>
        <isbn>0743416910</isbn>
        <price>5.99</price>
    </book>

    <!-- more books... -->

</inventory>

using DOM parsing:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(<uri_as_string>);
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression expr = xpath.compile(<xpath_expression>);

however, their purpose are mostly to get VALUE of some node(s) by tag or by attribute from the document.

My purpose is to get the entire XML STRING of the node(s) back. For example, using Xpath /inventory/book[@year='2005'], i want to get the following xml back in a single string, ie

    <book year="2005">
        <title>Burning Tower</title>
        <author>Larry Niven</author>
        <author>Jerry Pournelle</author>
        <publisher>Pocket</publisher>
        <isbn>0743416910</isbn>
        <price>5.99</price>
    </book>

What is the API used for this purpose? And do i even need the DOM parsing in this case? Thanks,

COMMENT:

Maybe I should emphasize that I am asking this question as a XML related one, not a text file processing question. Concepts like 'tag', 'attribute', 'Xpath' still apply. The DOM model is not totally irrelevant. It's just that instead of getting the 'element' or value of a node, i want to get the whole node.

The given answers can not solve problems like: how to get a node in xml string format, given the node's Xpath representation, such as //book or /inventory/book[1] ?

DOM parsers are designed to get values from the them not for actual file content.

You can use a simple file reader instead of XML.

Read line by line using a simple FileReader and check the line for the Condition and if the condition is met start the read content to concat as you want until the End of the node .

You can do it as

if(lineReadFromFile=="Your String Condition"){
    //collect the desired file content here untill the end of the Node is found
}

You can simply read XML from file (consider it to be a normal text file) using FileReader . Simple apply the condition for example :

if(line.equals("<book year="2005"><title>Burning Tower</title>")) {
     // retrieve/save the required content
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM