Solved

Reading an XML file from within an XML tag from Java

Posted on 2008-09-29
12
389 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
Superior storage. Superior surveillance.

WD Purple drives are built for 24/7, always-on, high-definition security systems. With support for up to 8 hard drives and 32 cameras, WD Purple drives are optimized for surveillance.

 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
DNS, website, godaddy 6 78
web services creation SOAP vs REST 5 20
couple of eclipse 5 25
factorial example 4 35
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
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.

867 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

21 Experts available now in Live!

Get 1:1 Help Now