Solved

Reading an XML file from within an XML tag from Java

Posted on 2008-09-29
12
404 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
Connect further...control easier

With the ATEN CE624, you can now enjoy a high-quality visual experience powered by HDBaseT technology and the convenience of a single Cat6 cable to transmit uncompressed video with zero latency and multi-streaming for dual-view applications where remote access is required.

 
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
 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Most ColdFusion developers get confused between the CFSet, Duplicate, and Structcopy methods of copying a Structure, especially which one to use when. This Article will explain the differences in the approaches with examples; therefore, after readin…
Introduction This article explores the design of a cache system that can improve the performance of a web site or web application.  The assumption is that the web site has many more “read” operations than “write” operations (this is commonly the ca…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

830 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