Solved

Reading an XML file from within an XML tag from Java

Posted on 2008-09-29
12
411 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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ejb wildfly example 2 113
add projects t working set in maven 2 71
Running JavaFX on the Raspberry Pi 27 299
Netbeans and org.apache.commons.lang3 issue 3 55
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 …
This video teaches viewers about errors in exception handling.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

751 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