将 XML 字符串转换为 org.w3c.dom.Document
Convert XML string to org.w3c.dom.Document?
我有以下XML字符串,我想将此字符串转换为org.w3c.dom.Document以获取"CallPaySecureResult"元素的值。
<?xml version="1.0" encoding="UTF-8"?>
<S:Body xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<ns2:CallPaySecureResponse xmlns:ns2="https://paysecure/merchant.soap/">
<CallPaySecureResult>&lt;status&gt;success&lt;/status&gt;&lt;errorcode&gt;0&lt;/errorcode&gt;&lt;errormsg /&gt;&lt;guid&gt;d785f819-6fc1-1c68-8edf-bbb65cba5412&lt;/guid&gt;&lt;/paysecure&gt;</CallPaySecureResult>
</ns2:CallPaySecureResponse>
</S:Body>
我尝试了以下代码
public String processIssuerResultParameters(String strXML)throws Exception
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
Document doc=null;
String CallPaySecureResult ="";
try
{
builder = factory.newDocumentBuilder();
doc = builder.parse( new InputSource( new StringReader( strXML ) ) );
logger.severe(doc.getTextContent());
} catch (Exception e) {
return "1:"+e.toString()+doc.getTextContent();
}
}
我试过这个:
InputSource is= new InputSource(new StringReader(strXML));
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
builder = null;
builder = factory.newDocumentBuilder();
doc = builder.parse(is);
而这个:
Source source = new StreamSource(new StringReader(strXML));
DOMResult result = new DOMResult();
TransformerFactory.newInstance().newTransformer().transform(source , result);
doc = (Document) result.getNode();
但在所有情况下,变量"doc"都是空的。
如何将 XML(字符串)解析为文档并获取<CallPaySecureResult>
中的值?
以下是一些示例 + 上下文。
您应该查看整个文件:https://github.com/bcdev/ceres/blob/master/ceres-metadata/src/main/java/com/bc/ceres/metadata/XPathHandler.java#L76
private static Document transformToDocument(Object document) {
try {
String docString;
if (document instanceof ReaderResource) {
docString = ((ReaderResource) document).getContent();
} else if (document instanceof String) {
docString = (String) document;
} else if (document instanceof Element) { //used?
DOMSource domSource = new DOMSource((Element) document);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
Properties properties = new Properties();
properties.setProperty(OutputKeys.METHOD, "xml");
properties.setProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.setOutputProperties(properties);
transformer.transform(domSource, result);
docString = writer.toString();
} else {
return null;
}
InputStream is = new ByteArrayInputStream(docString.getBytes());
return DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(is);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
这可能有助于您:
public class Convert {
public static void main(String[] args) {
Document copyBookXml = XmlUtils.fileToDom(args[1]);
Document sourceDocument = XmlUtils.fileToDom(args[0]);
// params 1) XML source document 2) Copybook as XML
String result = new XmlToMainframe().convert(sourceDocument, copyBookXml);
FileUtils.writeFile(result, "mfresult.txt", false);
}
}
完整代码在这里(code.openhub.net):
http://code.openhub.net/file?fid=e0O83oBJw47fhoPbZm0MgKWWStk&cid=7VtqxYTpWXE&s=Convert%20XML%20string%20to%20org.w3c.dom.Document%3F&pp=0&fl=Java&ff=1&projSelected=false&filterChecked,=true&mp,=1&filterChecked=true&mp=1&ml=1&me=1&md=1#L14
尝试获取元素时需要使用命名空间,下面是工作代码:
private static String input =
"<?xml version='"1.0'" encoding='"UTF-8'"?>"
+"<S:Body xmlns:S='"http://schemas.xmlsoap.org/soap/envelope/'">"
+"<ns2:CallPaySecureResponse xmlns:ns2='"https://paysecure/merchant.soap/'">"
+"<CallPaySecureResult>&lt;status&gt;success&lt;/status&gt;&lt;errorcode&gt;0&lt;/errorcode&gt;&lt;errormsg /&gt;&lt;guid&gt;d785f819-6fc1-1c68-8edf-bbb65cba5412&lt;/guid&gt;&lt;/paysecure&gt;</CallPaySecureResult>"
+"</ns2:CallPaySecureResponse>"
+"</S:Body>";
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
docFactory.setNamespaceAware(true);
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
InputSource is= new InputSource(new StringReader(input));
Document doc = docBuilder.parse(is);
String docNS = "https://paysecure/merchant.soap/";
NodeList nl = doc.getElementsByTagNameNS(docNS, "CallPaySecureResponse");
String s = nl.getLength()> 0 ?nl.item(0).getTextContent():"ELEMENT NOT FOUND";
System.out.println(s);
输出:
<status>success</status><errorcode>0</errorcode><errormsg /><guid>d785f819-6fc1-1c68-8edf-bbb65cba5412</guid></paysecure>
希望这能有所帮助。
相关文章:
- 在单击任何位置时隐藏元素,而不检查每次DOM单击
- 是否有任何snippet或jQuery插件可以列出easylist.txt模式匹配的DOM中的所有元素
- 在不使用JQuery的情况下隐藏DOM中的选定元素
- 如何在DOM元素上按类型构建此函数
- Windows形成web浏览器控件和Javascript更改的DOM
- Datatables:通过DOM数据源中的名称引用列
- 在DOM中查找一个模式并替换它's的内容使用jquery
- DOM事件通过JSON转换为java
- W3C循环样式的JavaScript
- 将DOM节点值与字符串Javascript进行比较
- delete在Object上效率低下,但在DOM Element's的数据属性,与null out相比
- 流星中DOM的繁殖
- DOM元素和angular元素之间的主要区别是什么
- 当带有渲染器的DOM元素不在屏幕顶部时,移动了场景的坐标
- 如何将Knockout.JS与服务器已经在DOM中呈现的数据同步
- PHP-如何重定向到同一页面并更改DOM's
- 如何使用ViewCompiler手动编译DOM的一部分
- 从popup.js|Chrome扩展访问DOM
- 将 XML 字符串转换为 org.w3c.dom.Document
- w3c dom and the javascript