JDK 解析xml文件

来源:互联网 发布:软件测试简答题 编辑:程序博客网 时间:2024/05/22 12:18

在对xml的实际的应用中、有时候为了方便会使用java jdk默认的解析xml的方式,而不去使用插件来操作。废话不多说了,这个应用只适合初学者,高手就不用看了... ...

文件如下:

<?xml version="1.0" encoding="UTF-8"?>

<config>
    <GDF-featureClass>
        <featureClass>
            <name>Substation_Boundary</name>
            <export-name>SubstationRegion</export-name>
            <props>
                <prop propname="esriname" FeatureFieldName="SUB" />
            </props>
        </featureClass>    
        
    </GDF-featureClass>
</config>

操作方式如下:

InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream("GDF_PROP.xml");
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            factory.setNamespaceAware(true);
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.parse(input);
            Element root = document.getDocumentElement();
            NodeList list = root.getElementsByTagName("GDF-featureClass");
            Element element = (Element) list.item(0);
            NodeList titleList = element.getElementsByTagName("featureClass");
            List<ConfileInfo> confList = new ArrayList<ConfileInfo>();
            ConfileInfo fileInfo=null;
            int count = titleList.getLength();  
            for (int i = 0; i < count; i++) {
                fileInfo =  new ConfileInfo();
                Element featureClass = (Element) titleList.item(i);
                String featureClassName = featureClass.getElementsByTagName("name").item(0).getFirstChild().getNodeValue();
                String GDF_Defination_name = featureClass.getElementsByTagName("export-name").item(0).getFirstChild().getNodeValue();
                //props
                NodeList props = featureClass.getElementsByTagName("props");
                String esriname =null;
                String SUB =null;
                for (int j = 0; j < props.getLength(); j++) {
                    Element propert = (Element) props.item(j);
                     esriname =propert.getElementsByTagName("prop").item(0).getAttributes().item(1).getFirstChild().getNodeValue();
                     SUB = propert.getElementsByTagName("prop").item(0).getAttributes().item(0).getLastChild().getNodeValue();
                    fileInfo.getProps().put(esriname, SUB);
                }
                fileInfo.setFeatureClassname(featureClassName);
                fileInfo.setGDF_Defination_name(GDF_Defination_name);
                //add to confList
                confList.add(fileInfo);
            }


还有是String型的xml文件的格式,这个的处理就会好很多,如下:

            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();   
            DocumentBuilder builder  =  factory.newDocumentBuilder();  
            Document doc  =  builder.parse( new ByteArrayInputStream(loc.getBytes()));
            if (doc.getElementsByTagName("coordinates").item(0).getFirstChild()!=null) {
                String result = doc.getElementsByTagName("coordinates").item(0).getFirstChild().getNodeValue();          
}

如此就可以获得coordinates 节点下的xml文件内容。

XML的解析方式有很多种,但是jdk 自带的这种是比较效率低的,但是没有像其他的那样,需要加入jar包来调用。

根据使用项目的应用来选择使用哪种方式去使用。