Link to home
Start Free TrialLog in
Avatar of moorcroftlad
moorcroftlad

asked on

Reading an XML file from within an XML tag from Java

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.
Avatar of manuel_m
manuel_m
Flag of Germany image

What kind of object do you have when you get the SOAP response?

A SOAPDocument object or something else?

manuel
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

Avatar of moorcroftlad
moorcroftlad

ASKER

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

plus I'm using JSDK 1.5.1 so I dont have javax.xml.xpath...
SOLUTION
Avatar of manuel_m
manuel_m
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
You're welcome.