Java 从节点实例获取完整的xml文本
我读过Java中的XML文件,代码如下:Java 从节点实例获取完整的xml文本,java,xml,Java,Xml,我读过Java中的XML文件,代码如下: File file = new File("file.xml"); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(file); NodeList nodeLst = doc.getElementsByTagName("reco
File file = new File("file.xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(file);
NodeList nodeLst = doc.getElementsByTagName("record");
for (int i = 0; i < nodeLst.getLength(); i++) {
Node node = nodeLst.item(i);
...
}
File File=new文件(“File.xml”);
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder db=dbf.newDocumentBuilder();
文档doc=db.parse(文件);
NodeList nodeLst=doc.getElementsByTagName(“记录”);
对于(int i=0;i
那么,如何从节点实例获取完整的xml内容呢?(包括所有标签、属性等)
谢谢。请从stackoverflow查看另一个
您将使用(而不是StreamSource),并在构造函数中传递节点
然后可以将节点转换为字符串
快速样本:
public class NodeToString {
public static void main(String[] args) throws TransformerException, ParserConfigurationException, SAXException, IOException {
// just to get access to a Node
String fakeXml = "<!-- Document comment -->\n <aaa>\n\n<bbb/> \n<ccc/></aaa>";
DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = docBuilder.parse(new InputSource(new StringReader(fakeXml)));
Node node = doc.getDocumentElement();
// test the method
System.out.println(node2String(node));
}
static String node2String(Node node) throws TransformerFactoryConfigurationError, TransformerException {
// you may prefer to use single instances of Transformer, and
// StringWriter rather than create each time. That would be up to your
// judgement and whether your app is single threaded etc
StreamResult xmlOutput = new StreamResult(new StringWriter());
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.transform(new DOMSource(node), xmlOutput);
return xmlOutput.getWriter().toString();
}
}
公共类NodeToString{
公共静态void main(字符串[]args)抛出TransformerException、ParserConfiguration异常、SAXException、IOException{
//只是为了访问一个节点
字符串fakeXml=“\n\n\n\n”;
DocumentBuilder docBuilder=DocumentBuilderFactory.newInstance().newDocumentBuilder();
documentdoc=docBuilder.parse(新的InputSource(新的StringReader(fakeXml));
Node=doc.getDocumentElement();
//测试方法
System.out.println(node2String(node));
}
静态字符串node2String(节点节点)抛出TransformerFactoryConfigurationError,TransformerException{
//您可能更喜欢使用Transformer的单个实例,以及
//StringWriter而不是每次都创建。这将取决于您的
//判断你的应用程序是否是单线程的等等
StreamResult xmlOutput=新的StreamResult(新的StringWriter());
Transformer Transformer=TransformerFactory.newInstance().newTransformer();
setOutputProperty(OutputKeys.OMIT_XML_声明,“yes”);
transform(新的DOMSource(节点),xmlOutput);
返回xmlOutput.getWriter().toString();
}
}
您所说的“获取完整的xml内容”是什么意思?您希望得到什么类型的对象?一根绳子?还有什么?完整的xml内容将在file.xml中,还是我没有抓住要点?否则,请尝试或。@PaulGrime,您的意思是,我必须用XML序列化程序序列化“节点”实例吗?@JimGarrison,“获取完整的XML内容”是指下一步(例如):数据