我正在使用W3C DOM在Java语言中解析一个XML文件。我陷入了一个特定的问题,我不知道如何获取节点的整个内部XML。
该节点如下所示:
<td><b>this</b> is a <b>test</b></td>
我必须使用什么函数才能获得它:
"<b>this</b> is a <b>test</b>"
发布于 2012-03-13 17:48:50
你对此有何感想?我今天在android上也遇到了同样的问题,但我设法做了一个简单的“序列化程序”。
private String innerXml(Node node){
String s = "";
NodeList childs = node.getChildNodes();
for( int i = 0;i<childs.getLength();i++ ){
s+= serializeNode(childs.item(i));
}
return s;
}
private String serializeNode(Node node){
String s = "";
if( node.getNodeName().equals("#text") ) return node.getTextContent();
s+= "<" + node.getNodeName()+" ";
NamedNodeMap attributes = node.getAttributes();
if( attributes!= null ){
for( int i = 0;i<attributes.getLength();i++ ){
s+=attributes.item(i).getNodeName()+"=\""+attributes.item(i).getNodeValue()+"\"";
}
}
NodeList childs = node.getChildNodes();
if( childs == null || childs.getLength() == 0 ){
s+= "/>";
return s;
}
s+=">";
for( int i = 0;i<childs.getLength();i++ )
s+=serializeNode(childs.item(i));
s+= "</"+node.getNodeName()+">";
return s;
}https://stackoverflow.com/questions/484995
复制相似问题