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</Schema Version>
<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.
<?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</Schema
<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.
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();
}
ASKER
Yeh it is an org.w3c.dom.Document. I'll paste what code I have:
TransformerFactory tFact = TransformerFactory.newInst ance();
Transformer galvatron = tFact.newTransformer();
Source soapContent = response.getSOAPPart().get Content();
DOMResult result = new DOMResult();
galvatron.transform(soapCo ntent, result);
org.w3c.dom.Node msgRoot = result.getNode();
org.w3c.dom.Element rootEl = ((org.w3c.dom.Document)msg Root).getD ocumentEle ment();
org.w3c.dom.Document doc = (org.w3c.dom.Document)msgR oot;
Then to parse the code I've used:
if(getPathValue(doc,"//soa p:Envelope /soap:Body /GetAppXml Response/G etAppXmlRe sult/Conne ctor/getap pxml/scota pp:Proposa l/scotapp: Applicatio nHeader/sc otapp:RefN um").lengt h()>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.
TransformerFactory tFact = TransformerFactory.newInst
Transformer galvatron = tFact.newTransformer();
Source soapContent = response.getSOAPPart().get
DOMResult result = new DOMResult();
galvatron.transform(soapCo
org.w3c.dom.Node msgRoot = result.getNode();
org.w3c.dom.Element rootEl = ((org.w3c.dom.Document)msg
org.w3c.dom.Document doc = (org.w3c.dom.Document)msgR
Then to parse the code I've used:
if(getPathValue(doc,"//soa
{
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.
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";
}
ASKER
plus I'm using JSDK 1.5.1 so I dont have javax.xml.xpath...
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
The code you mentioned gave me the following error:
[Fatal Error] :-1:-1: Premature end of file.
org.xml.sax.SAXParseExcept ion: Premature end of file.
[Fatal Error] :-1:-1: Premature end of file.
org.xml.sax.SAXParseExcept
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
Thanks
You're welcome.
A SOAPDocument object or something else?
manuel