我有以下带有肥皂信封的 XML 作为 Java String
:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<MyStartElement xmlns="http://www.example.com/service">
...
我希望稍后能够在其上使用 hamcrest 和 xml-matchers 扩展https://code.google.com/p/xml-matchers,但首先我想摆脱肥皂信封。
如何使用 JDOM 2.0.5 删除肥皂信封并将剩余的 XML(即以MyStartElement
root 开头)作为String
.
我尝试了以下方法:
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)));
这将返回:线程“main”中的异常 org.jdom2.IllegalAddException:内容已经有一个现有的父级“soap:Body”
我有一个类似的设置,我打电话给
System.out.println(new XMLOutputter().outputString(new Document(response)));
但这会返回整个 XML,包括肥皂信封。
我需要做什么才能使用 JDOM 从我的 XML 中剥离肥皂信封并获得String
回报?
奖励问题:
- JDOM 2 有很好的介绍/教程吗?(该网站似乎只有 JavaDocs,这使得入门有点困难......)
- 我意识到使用 JDOM 可能有点过头了。关于如何以更简单的方式执行此操作的任何建议?