java读取Xml,写入Xml的简单Dom操作,忽略DTD验证

package com.yd.common;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.StringWriter;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import com.sun.org.apache.xml.internal.serialize.OutputFormat;
import com.sun.org.apache.xml.internal.serialize.XMLSerializer;

public class UpdateXml {

 private void doc2XmlFile(Document document, String filePath) {
  try {
   OutputFormat format = new OutputFormat(document);//Serialize DOM
     format.setEncoding("UTF-8");
     StringWriter stringOut = new StringWriter();//Writer will be a String
     XMLSerializer serial = new XMLSerializer(stringOut, format);
     serial.asDOMSerializer();// As a DOM Serializer
     serial.serialize(document.getDocumentElement());
     String STRXML = stringOut.toString(); //Spit out DOM as a String
     File f = new File(filePath);
     PrintWriter out = new PrintWriter(new FileWriter(f));
     out.print(STRXML + "/n");
     out.close();
  } catch (Exception ex) {
   ex.printStackTrace();
  }
 }

 private Document load(String filename) {
  Document document = null;
  try {
   DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
   factory.setValidating(false);
   DocumentBuilder builder = factory.newDocumentBuilder();
   builder.setEntityResolver(this.new IgnoreDTDEntityResolver());
   document = builder.parse(new File(filename));
   document.normalize();
  } catch (Exception ex) {
   ex.printStackTrace();
  }
  return document;
 }

 /**
  * 演示修改文件的具体某个节点的值
  */
 public void xmlUpdateDemo(String filePath, String customerName, String DMSID) {
  Document document = load(filePath);
  Element root = document.getDocumentElement();
  /** 如果root有子元素 */
  if (root.hasChildNodes()) {
   /** ftpnodes */
   NodeList ftpnodes = root.getChildNodes();

   /** 循环取得ftp所有节点 */
   for (int i = 0; i < ftpnodes.getLength(); i++) {
    Node subnode = ftpnodes.item(i);

    if (subnode.getNodeType() == Node.ELEMENT_NODE && subnode.getNodeName() == "Details") {
     subnode.getAttributes().getNamedItem("DMSID").setNodeValue(DMSID);
    }
    if (subnode.getNodeType() == Node.ELEMENT_NODE && subnode.getNodeName() == "CustomerDetails") {
     subnode.getAttributes().getNamedItem("CustomerName").setNodeValue(customerName);
    }
   }
  }
  doc2XmlFile(document, filePath);
 }
 /**
  * 忽略DTD验证文件子类
  */
    class IgnoreDTDEntityResolver implements EntityResolver{
        public InputSource resolveEntity(java.lang.String publicId, java.lang.String systemId) throws SAXException,
                java.io.IOException{
           if (systemId.contains("NOCServerDetails.dtd")){
            InputSource is = new InputSource(new ByteArrayInputStream("<?xml version=/"1.0/" encoding=/"UTF-8/"?>".getBytes()));
            return is;
           }
            else
                return null;
        }
    }

 public static void main(String args[]) throws Exception {
  new UpdateXml().xmlUpdateDemo("C://testop_Key//NOCServerDetails.xml","3333","3333");

 }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值