document 與String之間的互相轉換
字串轉換為 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; }