?
Solved

Reading an XML file from within an XML tag from Java

Posted on 2008-09-29
12
Medium Priority
?
421 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
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 2000 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 2000 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 2000 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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
What You Need to Know when Searching for a Webhost Provider
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

718 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