将xml内容转化为String
彭令秋
2023-12-01
1 dom4j中的Document有一个方法。asXML()。该方法是将xml文件内容转化为字符串。
如:SAXReader saxReader=new SAXReader();
Document document;
String xmlString="";
try {
document = saxReader.read(new File(xmlFileName));
xmlString=document.asXML();//将xml内容转化为字符串
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
xmlString="";
}
//String xmlString=document.asXML();
return xmlString;
综述:asXML()可以讲xml文件内容转化为字符串。
完整的例子:
String xmlFileName="D:\\工作_中电一体化\\AppRouter\\router\\adapter\\adaptorAttributes.xml";
String xmlString=new TestMain().xmlToString(xmlFileName);
System.out.println(xmlString);
public String xmlToString(String xmlFileName){
SAXReader saxReader=new SAXReader();
Document document;
String xmlString="";
try {
document = saxReader.read(new File(xmlFileName));
xmlString=document.asXML();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
xmlString="";
}
//String xmlString=document.asXML();
return xmlString;
}