How to use XML inside a String variable

Asked

Viewed 68 times

0

I created a variable of type String that has the return of the webservice in XML:

Example:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<books>
    <book>
        <author>
            <firstName>Bert</firstName>
            <id>1</id>
            <lastName>Bates</lastName>
        </author>
        <isbn>ISBN-45565-45</isbn>
        <title>Head First Java</title>
    </book>
</books> 

If I want to get the value "ISBN-45565-45" is inside the tag, how to proceed??? I am using java.

2 answers

1

I could search inside the string, but it is not the best way, if the string is a valid XML document, better convert to an XML Document and search the value inside the tag using getElementsByTagName.

So for example:

String xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" +
                        "<books>" +
                        "   <book>" +
                        "       <author>" +
                        "           <firstName>Bert</firstName>" +
                        "           <id>1</id>" +
                        "           <lastName>Bates</lastName>" +
                        "       </author>" +
                        "       <isbn>ISBN-45565-45</isbn>" +
                        "       <title>Head First Java</title>" +
                        "   </book>" +
                        "</books>";


        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();  
        DocumentBuilder builder;  
        try  
        {  
            builder = factory.newDocumentBuilder();  
            Document doc = builder.parse( new InputSource( new StringReader( xmlStr ) ) ); 

            var node = doc
                .getElementsByTagName("isbn")
                .item(0)
                .getTextContent();
                
            System.out.println(node); 
                
        } catch (Exception e) {  
            System.out.println(e);  
        } 

You can see it working here: https://www.mycompiler.io/view/7oNNmFZ

0

To answer the question, there are some ways, one that I find sensible and robust would be to look for a good library to do Parsing from XML to Java, possibly some that is already available in the standard language library, and learn how to use it to parse your XML. I suggest as keywords for this in the search engine of your preference XML parser Java library. I will not give suggestions because this information changes and would leave the answer outdated.

Although some certainly provide features to automatically browse XML in search of what you want, the most likely way to use will be you, upon prior knowledge of the parts that make up the XML tree (I only found this information in English, for example here) and the format of the XML data coming in the web service, specify via programming when using the library by which parts you need to navigate in XML until you find the desired information (for example, in your case you would have to specify to enter the root element books, inside navigate through each (Obs.: cacophagus) one of the elements book and from each extract the text of the element isbn contained in it). Also keep in mind that, because it is a tree type data structure, typical terms of this structure can be used in the library such as Node or knot.

For small data sets like conventional returns from a web service there is no problem if the library performs all the loading and processing directly in memory, whereas in the case of larger XML’s it may be interesting to search for a more robust, to do a gradual loading and processing. That would be more for archives.

Other ways that are generally less robust to do, but can serve if you are sure of the data pattern that will always come, would be to fetch and extract the information you want using regular expressions or search inside the string for substrings. But like I said, it’s more plastered and less maintainable in the general case.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.