`

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;
    }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics