I have the following XML with a soap envelope as a Java String
:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<MyStartElement xmlns="http://www.example.com/service">
...
I want to be able to use hamcrest and the xml-matchers extension https://code.google.com/p/xml-matchers on it later on, but first I want to get rid of the soap envelope.
How can I remove the soap envelope using JDOM 2.0.5 and get the remaining XML (i.e. starting with the MyStartElement
as root) back as a String
?
I tried the following:
SAXBuilder builder = new SAXBuilder();
Document document = (Document) builder.build(toInputStream(THE_XML));
Namespace ns = Namespace
.getNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/");
Namespace ns2 = Namespace
.getNamespace("http://www.example.com/service");
Element response = document.getRootElement()
.getChild("Body", ns)
.getChild("MyStartElement", ns2);
System.out.println(new XMLOutputter().outputString(new Document(response)));
This returns: Exception in thread "main" org.jdom2.IllegalAddException: The Content already has an existing parent "soap:Body"
I had a similar setup, where I called
System.out.println(new XMLOutputter().outputString(new Document(response)));
but that returned the whole XML including soap envelope.
What would I have to do to strip the soap envelope from my XML using JDOM and get a String
back?
Bonus questions:
- Is there a good introduction/tutorial to JDOM 2? (The website seems to only have the JavaDocs, which makes it a bit difficult to get started...)
- I realise using JDOM is probably a bit over the top for this one. Any suggestions on how to do this in an easier way?