字符串转换为 Document
public static org.w3c.dom.Document getStringToDocument(String svg){
StringReader sr = new StringReader(svg);
InputSource is = new InputSource(sr);
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
org.w3c.dom.Document document = null;
try {
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.parse(is);//获得document
Element element = document.getDocumentElement();
String height = element.getAttribute(“height”);//获取其中的某个元素
String width = element.getAttribute(“width”);
element.setAttribute(“height”, “2000”);//修改其中的某个元素
element.setAttribute(“width”, “3000”);
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return document;
}
Document 转换为String字符串
private String DocumentToString(org.w3c.dom.Document document) {
String asa = org.apache.axis.utils.XMLUtils.DocumentToString(document);
return asa;
}