QQ在线咨询
售前咨询热线
15821572819
售后咨询热线
15821572819

java修改xml节点方法


1.最简单的修改

[html] view plain copy

  1. import java.io.FileOutputStream;  

  2.   

  3. import javax.xml.parsers.DocumentBuilder;  

  4. import javax.xml.parsers.DocumentBuilderFactory;  

  5. import javax.xml.transform.OutputKeys;  

  6. import javax.xml.transform.Transformer;  

  7. import javax.xml.transform.TransformerFactory;  

  8. import javax.xml.transform.dom.DOMSource;  

  9. import javax.xml.transform.stream.StreamResult;  

  10.   

  11. import org.w3c.dom.Document;  

  12. import org.w3c.dom.Element;  

  13. import org.w3c.dom.NodeList;  

  14.   

  15. public class Test {  

  16.     public static void main(String[] args) {  

  17.         try {  

  18.             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();  

  19.   

  20.             DocumentBuilder db = dbf.newDocumentBuilder();  

  21.   

  22.             Document doc = db.parse(XmlUtil.getCurrentPath());  

  23.   

  24.             NodeList list = doc.getElementsByTagName("entry");  

  25.             for (int i = 0; i < list.getLength(); i++) {  

  26.                 Element ele = (Element) list.item(i);  

  27.                 String brandName = ele.getAttribute("key");  

  28.                 if (brandName.equals("outRoot")) {  

  29.                     ele.setTextContent("${user.dir}/generator-output/123");  

  30.                 }  

  31.             }  

  32.   

  33.             TransformerFactory transformerFactory = TransformerFactory.newInstance();  

  34.             Transformer transformer = transformerFactory.newTransformer();  

  35.             DOMSource domSource = new DOMSource(doc);  

  36.             // 设置编码类型  

  37.             transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");  

  38.             StreamResult result = new StreamResult(new FileOutputStream(XmlUtil.getCurrentPath()));  

  39.             transformer.transform(domSource, result);  

  40.         } catch (Exception ex) {  

  41.             ex.printStackTrace();  

  42.         }  

  43.     }  

  44. }  

2.稍微不一样的


[html] view plain copy

  1. import java.io.Console;  

  2. import java.io.File;  

  3. import java.io.IOException;  

  4.   

  5. import javax.xml.parsers.DocumentBuilder;  

  6. import javax.xml.parsers.DocumentBuilderFactory;  

  7. import javax.xml.transform.Transformer;  

  8. import javax.xml.transform.TransformerFactory;  

  9. import javax.xml.transform.dom.DOMSource;  

  10. import javax.xml.transform.stream.StreamResult;  

  11. import javax.xml.xpath.XPath;  

  12. import javax.xml.xpath.XPathConstants;  

  13. import javax.xml.xpath.XPathExpressionException;  

  14. import javax.xml.xpath.XPathFactory;  

  15.   

  16. import org.w3c.dom.Document;  

  17. import org.w3c.dom.Element;  

  18. import org.w3c.dom.Node;  

  19.   

  20.   

  21. public class XmlUtil {  

  22.     public static void main(String[] args) throws IOException {  

  23.         modifySon("test");  

  24.     }  

  25.       

  26.     public static void modifySon(String newPackage){  

  27.         DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();  

  28.         dbf.setIgnoringElementContentWhitespace(true);  

  29.         try{  

  30.             DocumentBuilder db=dbf.newDocumentBuilder();  

  31.             Document xmldoc=db.parse(XmlUtil.getCurrentPath());  

  32.               

  33.             Element root = xmldoc.getDocumentElement();  

  34.             System.out.println(root.toString());  

  35.               

  36.             Element per =(Element) selectSingleNode("/properties/entry[@key='outRoot']", root);  

  37.             per.setTextContent("${user.dir}/generator-output/"+newPackage);  

  38.             TransformerFactory factory = TransformerFactory.newInstance();  

  39.             Transformer former = factory.newTransformer();  

  40. //            former.setOutputProperty(javax.xml.transform.OutputKeys.DOCTYPE_PUBLIC, xmldoc.getDoctype().getPublicId());    

  41.             former.setOutputProperty(javax.xml.transform.OutputKeys.DOCTYPE_SYSTEM, xmldoc.getDoctype().getSystemId());  

  42.             former.transform(new DOMSource(xmldoc), new StreamResult(new File(XmlUtil.getCurrentPath())));  

  43.         }catch(Exception e){  

  44.             e.printStackTrace();  

  45.         }  

  46.     }  

  47.     public static Node selectSingleNode(String express, Element source) {  

  48.         Node result=null;  

  49.         XPathFactory xpathFactory=XPathFactory.newInstance();  

  50.         XPath xpath=xpathFactory.newXPath();  

  51.         try {  

  52.             result=(Node) xpath.evaluate(express, source, XPathConstants.NODE);  

  53.         } catch (XPathExpressionException e) {  

  54.             e.printStackTrace();  

  55.         }  

  56.           

  57.         return result;  

  58.     }  

  59.       

  60.     public static String getCurrentPath(){   

  61.         String path = XmlUtil.class.getProtectionDomain().getCodeSource().getLocation().getPath();  

  62.         String rootPath = path.substring(1,path.length()-5)+"/src/test.xml";  

  63.         return rootPath;           

  64.     }  

  65.       

  66. }  

特殊:其中xml文件中有DTD声明修改后没了

注意其中2句代码

[html] view plain copy

  1. //            former.setOutputProperty(javax.xml.transform.OutputKeys.DOCTYPE_PUBLIC, xmldoc.getDoctype().getPublicId());    

  2.             former.setOutputProperty(javax.xml.transform.OutputKeys.DOCTYPE_SYSTEM, xmldoc.getDoctype().getSystemId());  

根据声明类型计入对应的忽略设置




注:本文转载自csdn,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。如有侵权行为,请联系我们,我们会及时删除。
上一篇 下一篇

分享