我有一個XML org.w3c.dom.Node中,看起來像這樣:獲取節點的內部XML作爲字符串在Java中的DOM
<variable name="variableName">
<br /><strong>foo</strong> bar
</variable>
如何獲得<br /><strong>foo</strong> bar
部分作爲字符串?
我有一個XML org.w3c.dom.Node中,看起來像這樣:獲取節點的內部XML作爲字符串在Java中的DOM
<variable name="variableName">
<br /><strong>foo</strong> bar
</variable>
如何獲得<br /><strong>foo</strong> bar
部分作爲字符串?
org.w3c.dom.Node沒有簡單的方法。 getTextContent()將每個子節點的文本連接在一起。 getNodeValue()將爲您提供當前節點的文本,如果它是Attribute,CDATA或Text節點。因此,您需要使用getChildNodes(),getNodeName()和getNodeValue()的組合來序列化節點以構建字符串。
您也可以使用存在的各種XML序列化庫之一來執行此操作。有XStream甚至JAXB。這在XML serialization in Java?
中討論相同的問題。爲了解決這個問題,我寫這個輔助功能:
public String innerXml(Node node) {
DOMImplementationLS lsImpl = (DOMImplementationLS)node.getOwnerDocument().getImplementation().getFeature("LS", "3.0");
LSSerializer lsSerializer = lsImpl.createLSSerializer();
NodeList childNodes = node.getChildNodes();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < childNodes.getLength(); i++) {
sb.append(lsSerializer.writeToString(childNodes.item(i)));
}
return sb.toString();
}
,如果你不希望訴諸外部庫,以下解決方案可能會派上用場。如果你有一個節點「」你要提取的父元素的兒童遊樂步驟如下:
StringBuilder resultBuilder = new StringBuilder();
// Get all children of the given parent node
NodeList children = parent.getChildNodes();
try {
// Set up the output transformer
TransformerFactory transfac = TransformerFactory.newInstance();
Transformer trans = transfac.newTransformer();
trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
trans.setOutputProperty(OutputKeys.INDENT, "yes");
StringWriter stringWriter = new StringWriter();
StreamResult streamResult = new StreamResult(stringWriter);
for (int index = 0; index < children.getLength(); index++) {
Node child = children.item(index);
// Print the DOM node
DOMSource source = new DOMSource(child);
trans.transform(source, streamResult);
// Append child to end result
resultBuilder.append(stringWriter.toString());
}
} catch (TransformerException e) {
//Errro handling goes here
}
return resultBuilder.toString();
如果您使用jOOX,你可以用你的節點在jquery樣的語法,只是通話toString()
它:
$(node).toString();
它使用的身份變壓器內部,就像這樣:
ByteArrayOutputStream out = new ByteArrayOutputStream();
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
Source source = new DOMSource(element);
Result target = new StreamResult(out);
transformer.transform(source, target);
return out.toString();
大廈的盧卡斯埃德爾的解決方案上面,我們可以在.NET提取innerXml等作爲下面
public static String innerXml(Node node,String tag){
String xmlstring = toString(node);
xmlstring = xmlstring.replaceFirst("<[/]?"+tag+">","");
return xmlstring;
}
public static String toString(Node node){
String xmlString = "";
Transformer transformer;
try {
transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
//transformer.setOutputProperty(OutputKeys.INDENT, "yes");
StreamResult result = new StreamResult(new StringWriter());
xmlString = nodeToStream(node, transformer, result);
} catch (TransformerConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TransformerFactoryConfigurationError e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TransformerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (Exception ex){
ex.printStackTrace();
}
return xmlString;
}
例:
If Node name points to xml with string representation "<Name><em>Chris</em>tian<em>Bale</em></Name>"
String innerXml = innerXml(name,"Name"); //returns "<em>Chris</em>tian<em>Bale</em>"
我與最後應答該方法 'nodeToStream()' 是未定義的問題;因此,我的版本在這裏:
public static String toString(Node node){
String xmlString = "";
try {
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
//transformer.setOutputProperty(OutputKeys.INDENT, "yes");
Source source = new DOMSource(node);
StringWriter sw = new StringWriter();
StreamResult result = new StreamResult(sw);
transformer.transform(source, result);
xmlString = sw.toString();
} catch (Exception ex) {
ex.printStackTrace();
}
return xmlString;
}
擴展Andrey M的答案,我不得不略微修改代碼以獲取完整的DOM文檔。如果你只是使用
NodeList childNodes = node.getChildNodes();
它沒有包含我的根元素。以包括的根元素(和獲得完整的.xml文件)我使用:
public String innerXml(Node node) {
DOMImplementationLS lsImpl = (DOMImplementationLS)node.getOwnerDocument().getImplementation().getFeature("LS", "3.0");
LSSerializer lsSerializer = lsImpl.createLSSerializer();
lsSerializer.getDomConfig().setParameter("xml-declaration", false);
StringBuilder sb = new StringBuilder();
sb.append(lsSerializer.writeToString(node));
return sb.toString();
}
下面是一個替代的解決方案,以提取org.w3c.dom.Node中的內容。 此解決方案也該節點的內容不包含XML標籤:
private static String innerXml(Node node) throws TransformerFactoryConfigurationError, TransformerException {
StringWriter writer = new StringWriter();
String xml = null;
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.transform(new DOMSource(node), new StreamResult(writer));
// now remove the outer tag....
xml = writer.toString();
xml = xml.substring(xml.indexOf(">") + 1, xml.lastIndexOf("</"));
return xml;
}
感謝,正是我需要的 – yossi 2011-05-25 13:33:46
這種方法保持在字符串的前面添加XML定義標籤......有什麼辦法來防止除了之後簡單地修剪它? – Nyerguds 2011-08-08 09:58:38
我解決了它。解決方法是添加'lsSerializer.getDomConfig()。setParameter(「xml-declaration」,false);' – Nyerguds 2011-08-08 10:27:27