import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileWriter; import java.io.IOException; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.apache.batik.dom.svg.SAXSVGDocumentFactory; import org.apache.batik.util.XMLResourceDescriptor; import org.w3c.dom.Attr; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class DomTree { public Document doc; public Element svg; public DomTree(File file){ String parser=XMLResourceDescriptor.getXMLParserClassName(); SAXSVGDocumentFactory fac=new SAXSVGDocumentFactory(parser); if(doc == null) { //异常点............. //TMD 这个参数还有格式要求.... NND //With Batik, you can also create an SVG DOM tree from a URI, an InputStream, or a Reader, try{ doc=fac.createDocument(file.toURI().toString()); doc.normalize();//去掉XML文档中作为格式化内容的空白而映射在DOM树中的不必要的Text Node对象 svg = doc.getDocumentElement(); }catch(Exception e){ System.out.println("exception"); } } } public Boolean addNode(Element e) throws TransformerException{ svg.appendChild(e); analyseDoc(doc); File file=new File("test.xml"); try { saveDoc(file); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } return true; } private boolean saveDoc(File file) throws TransformerException, IOException{ TransformerFactory tfactory = TransformerFactory.newInstance(); Transformer transformer = tfactory.newTransformer(); // 将DOM对象转化为DOMSource类对象,该对象表现为转化成别的表达形式的信息容器。 DOMSource source = new DOMSource(doc); /* 获得一个StreamResult类对象,该对象是DOM文档转化成的其他形式的文档的容器, 可以是XML文件,文本文件,HTML文件。这里为一个XML文件。*/ // String filename="text.xml"; // // StreamResult result = new StreamResult(new File(filename.trim())); /* * 设置输出属性 * encoding = "GB2312" 代表 输出的编码格式为 GB2312 * indent = "yes" 代表缩进输出 */ transformer.setOutputProperty(OutputKeys.ENCODING,"GB2312"); transformer.setOutputProperty(OutputKeys.INDENT,"yes"); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); //transformer.transform()方法 将 XML Source转换为 Result // 调用API,将DOM文档转化成XML文件。 transformer.transform(source,new StreamResult(outputStream)); String ssvg=outputStream.toString(); System.out.println(ssvg); saveString(file,ssvg); return true; } private boolean saveString(File file,String s) throws IOException{ FileWriter fw=new FileWriter(file,false); fw.write(s); fw.close(); return true; } private void analyseDoc(Document doc) { Node root = doc.getDocumentElement(); analyseNode(root); } private void analyseNode(Node node){ if(node == null){ return; } System.out.println("----Node: "+node.getNodeName()+"----Start-----"); System.out.println("Node: "+node.getNodeName()+" : "+node.getNodeValue()+"#"); NamedNodeMap attrMap = node.getAttributes(); if(attrMap==null){ return; } for(int k=0; k<attrMap.getLength(); k++){ Attr anode = (Attr)attrMap.item(k); System.out.println(k+" Attr: "+anode.getName()+" : "+anode.getValue()+"#"); } if(node.hasChildNodes()){ NodeList onenodes = node.getChildNodes(); for(int i=0; i<onenodes.getLength(); i++){ analyseNode(onenodes.item(i)); } } System.out.println("----Node: "+node.getNodeName()+"----End-----"); } }