Solved

Reading an XML file from within an XML tag from Java

Posted on 2008-09-29
12
373 Views
Last Modified: 2013-11-24
Hi I have an XML SOAP response which starts with the following XML:

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetAppXmlResponse xmlns="http://***.***.gov.uk/***">
<GetAppXmlResult>
<Connector timestamp="9/26/2008 10:46:26 AM" xmlns="">
<code>0</code>
<message>Success</message>
<getappxml>
<![CDATA[<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Proposal>
<SchemaVersion>1.1</SchemaVersion>
<ApplicationHeader>Sample Header</ApplicationHeader>
 
//further nodes and elements
 
</Proposal>
]]>
</getappxml>
</Connector>
</GetAppXmlResult>
</GetAppXmlResponse>
</soap:Body>
</soap:Envelope>

I need to access the proposal node to get the RefNum although I'm not sure how to do this is Java.
0
Comment
Question by:moorcroftlad
  • 7
  • 5
12 Comments
 
LVL 6

Expert Comment

by:manuel_m
ID: 22596020
What kind of object do you have when you get the SOAP response?

A SOAPDocument object or something else?

manuel
0
 
LVL 6

Expert Comment

by:manuel_m
ID: 22596074
If you have an org.w3c.dom.Document or Node you could use Xpath to get your value.
javax.xml.xpath.XPathXPath xpath = javax.xml.xpath.XPathFactory.newInstance().newXPath();
 

// node is your xml org.w3c.dom.Document object or org.w3c.dom.Node 

try {

String value = (String) xpath.compile("//Proposal/RefNum").evaluate(node, XPathConstants.STRING);

} catch (javax.xml.xpath.XPathExpressionException e) {

e.printStackTrace();

}

Open in new window

0
 

Author Comment

by:moorcroftlad
ID: 22596119
Yeh it is an org.w3c.dom.Document.  I'll paste what code I have:

                    TransformerFactory tFact = TransformerFactory.newInstance();
                    Transformer galvatron = tFact.newTransformer();
                    Source soapContent = response.getSOAPPart().getContent();
                    DOMResult result = new DOMResult();
                    galvatron.transform(soapContent, result);
                   
                    org.w3c.dom.Node msgRoot = result.getNode();
                    org.w3c.dom.Element rootEl = ((org.w3c.dom.Document)msgRoot).getDocumentElement();
                    org.w3c.dom.Document doc = (org.w3c.dom.Document)msgRoot;

Then to parse the code I've used:

            if(getPathValue(doc,"//soap:Envelope/soap:Body/GetAppXmlResponse/GetAppXmlResult/Connector/getappxml/scotapp:Proposal/scotapp:ApplicationHeader/scotapp:RefNum").length()>0)
                                          {
                  strVersion="0.8";
            }

Although I think the problem is due to the fact that there is the !CDATA tag at the start of the inner xml file, if you take at look at it again.
0
 
LVL 6

Expert Comment

by:manuel_m
ID: 22596193
ok. missed the CDATA thing :-)

You have to get the content of the <getappxml> tag and then you can create a Document from it. After this you can easily read out the RefNum value.
// where xmlText your value from the getappxml tag is.

Document getAppDocument = null;

try {

            DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();

            getAppDocument = builder.parse(new InputSource(new StringReader(xmlText)));

        } catch (SAXException e) {

            throw new UtilRuntimeException("", e);

        } catch (IOException e) {

            throw new UtilRuntimeException("", e);

        } catch (ParserConfigurationException e) {

            throw new UtilRuntimeException("", e);

        }
 

if(getPathValue(getAppDocument,"//Proposal/ApplicationHeader/RefNum").length()>0) {

                  strVersion="0.8";

            }

Open in new window

0
 

Author Comment

by:moorcroftlad
ID: 22596200
plus I'm using JSDK 1.5.1 so I dont have javax.xml.xpath...
0
 
LVL 6

Assisted Solution

by:manuel_m
manuel_m earned 500 total points
ID: 22596209
you should also check if getAppDocument is not null...

manuel
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:moorcroftlad
ID: 22596263
sorry still don't really understand, I have a Document object (doc) so how does that code you just type create a new document object when it doesnt even reference this.  Can you explain further what the string xmlText is?

Thanks
0
 
LVL 6

Accepted Solution

by:
manuel_m earned 500 total points
ID: 22596482
The xmlText is the String value from the getappxml element/node.
I think you would get the value with your method

String xmlText = getPathValue(doc,"//soap:Envelope/soap:Body/GetAppXmlResponse/GetAppXmlResult/Connector/getappxml/scotapp:Proposal/");

The steps are:
1. Get the value of the getappxml node.
2. Create a Document object of the string value extracted from step 1.
3. get the value from RefNum from the created document in step 2.
// get the xml string from the getappxml node

String xmlText = getPathValue(doc,"//soap:Envelope/soap:Body/GetAppXmlResponse/GetAppXmlResult/Connector/getappxml");

Document getAppDocument = null;

try {

            DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();

// create a new document object from the string.

            getAppDocument = builder.parse(new InputSource(new StringReader(xmlText)));

        } catch (SAXException e) {

            throw new UtilRuntimeException("", e);

        } catch (IOException e) {

            throw new UtilRuntimeException("", e);

        } catch (ParserConfigurationException e) {

            throw new UtilRuntimeException("", e);

        }
 

// get the RefNum value from the newly created document.

if(getPathValue(getAppDocument,"//Proposal/ApplicationHeader/RefNum").length()>0) {

                  strVersion="0.8";

            }

Open in new window

0
 

Author Comment

by:moorcroftlad
ID: 22596869
The code you mentioned gave me the following error:

[Fatal Error] :-1:-1: Premature end of file.
org.xml.sax.SAXParseException: Premature end of file.
0
 
LVL 6

Assisted Solution

by:manuel_m
manuel_m earned 500 total points
ID: 22597089
Where does the error occur? Could you please post the complete error message?
0
 

Author Closing Comment

by:moorcroftlad
ID: 31501153
It worked out in the end, I just had to change soap:Envelope to soap-env:Envelope as I am working with an older version of the soap toolkit!

Thanks
0
 
LVL 6

Expert Comment

by:manuel_m
ID: 22603461
You're welcome.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Meet the world's only “Transparent Cloud™” from Superb Internet Corporation. Now, you can experience firsthand a cloud platform that consistently outperforms Amazon Web Services (AWS), IBM’s Softlayer, and Microsoft’s Azure when it comes to CPU and …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now