XMLHelperByDOM

package GoodSrc;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
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.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;
import org.xml.sax.InputSource;
/**
* 该操作类都采用DOM去解析XML文件
* @author gKF26601
*
*/
public class XMLHelperByDOM
{
/** 以下操作都以该XML文件为例子
<?xml version="1.0" encoding="iso-8859-1"?>
<root>
<InfoSet setID="3010" name="CSOFTX3000数据配置信息">
<InfoItem infoID="103707" type="xbgd" associate="0">
<me>CSOFTX3000</me>
<name>LICENSE信息</name>
<outputPath>CSOFTX3000/InfoCollect</outputPath>
<outputFile>license_info.xml</outputFile>
<operate operAtom="NeMML">
<Command saveReport="1">STR LICCRC:;</Command>
</operate>
</InfoItem>
<InfoItem infoID="301002" type="MMLItem" associate="1">
<me>CSOFTX3000</me>
<name>网关状态</name>
<outputPath>CSOFTX3000/InfoCollect</outputPath>
<outputFile>dsp_mgw.xml</outputFile>
<operate operAtom="MMLPython">
<Command cmdFile="dsp_mgwcmd.txt">healthcheck/dsp_mgw.py</Command>
</operate>
</InfoItem>
</InfoSet>
<InfoSet setID="1011" name="基本信息">
<InfoItem infoID="101507" type="MMLItem" associate="0">
<me>CGP</me>
<name>导出大文件列表</name>
<outputPath>CGP/MML</outputPath>
<outputFile>CGP_EXP_LARGEFILE.xml</outputFile>
<operate operAtom="NeMML">
<Command saveReport="1">EXP LARGEFILE: SIZE=10;</Command>
</operate>
</InfoItem>
<InfoItem infoID="101001" type="MMLItem" associate="0">
<me>CGP</me>
<name>网元对象实例</name>
<outputPath>CGP/MML</outputPath>
<outputFile>CGP_LST_ME.xml</outputFile>
<operate operAtom="NeMML">
<Command saveReport="1">LST ME:;</Command>
</operate>
</InfoItem>
<InfoItem infoID="101002" type="MMLItem" associate="0">
<me>CGP</me>
<name>联网方式</name>
<outputPath>CGP/MML</outputPath>
<outputFile>CGP_LST_NET.xml</outputFile>
<operate operAtom="NeMML">
<Command saveReport="1">LST NET:;</Command>
</operate>
</InfoItem>
</InfoSet>
</root>
*/

/**
* 返回XML的document
* @param file
* @return
*/
public static Document ReadXmlToDoc(String file)
{
/**
* 扩展参数的xml文件的DOC
*/
Document doc = null;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;

try
{
builder = factory.newDocumentBuilder();
InputSource inp = new InputSource(file);

// TODO 如果是中文,则采用GB2312编码格式
// inp.setEncoding("GB2312");

doc = builder.parse(inp);
}
catch (Exception e)
{
doc = null;
return null;
}
return doc;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值