| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 698 人关注过本帖
标题:《Java 核心技术》XML 文件解析的示例程序
只看楼主 加入收藏
日知己所无
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:38
帖 子:427
专家分:2071
注 册:2014-3-22
结帖率:92.86%
收藏
已结贴  问题点数:20 回复次数:3 
《Java 核心技术》XML 文件解析的示例程序
参考:《Java核心技术卷II:高级特性》第7版第12章XML

【完整的程序代码】
程序代码:
import import import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

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

public class ParsingXMLDocument {

    public static void main(String[] args) {
        try {
            parsingXMLDocument("C:/xml_file.xml");
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void parsingXMLDocument(final String XML_FILE_PATH)
            throws ParserConfigurationException, SAXException, IOException {
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();

        File file = new File(XML_FILE_PATH);
        if (!(file.exists() && file.isFile())) { return; }

        Document document = documentBuilder.parse(file);
        Element rootElement = document.getDocumentElement();

        System.out.println("<" + rootElement.getNodeName() + ">");
        NodeList rootElementChildNodeList = rootElement.getChildNodes();
        for (int rootElementChildNodeIndex = 0;
                 rootElementChildNodeIndex < rootElementChildNodeList.getLength();
                 rootElementChildNodeIndex++) {

            Node node = rootElementChildNodeList.item(rootElementChildNodeIndex);
            if (node instanceof Element) {
                Element nodeElement = (Element) node;

                System.out.println("\t<" + nodeElement.getNodeName() + ">");
                NodeList nodeElementChildNodeList = nodeElement.getChildNodes();
                for (int nodeElementChildNodeIndex = 0;
                         nodeElementChildNodeIndex < nodeElementChildNodeList.getLength();
                         nodeElementChildNodeIndex++) {

                    Node childNode = nodeElementChildNodeList.item(nodeElementChildNodeIndex);
                    if (childNode instanceof Element) {
                        Element childNodeElement = (Element) childNode;
                        System.out.println(
                                "\t\t<" + childNodeElement.getNodeName() + ">" +
                                childNodeElement.getTextContent() +
                                "</" + childNodeElement.getNodeName() + ">"
                                );
                    }
                }
                System.out.println("\t</" + nodeElement.getNodeName() + ">");
            }
        }
        System.out.println("</" + rootElement.getNodeName() + ">");
    }
}

【C:\xml_file.xml】的内容
程序代码:
<?xml version="1.0" encoding="UTF-8"?>
<!-- This is a comment. -->
<root>
    <node>
        <key1>value1</key1>
        <key2>value2</key2>
    </node>
    <node>
        <key1>value3</key1>
        <key2>value4</key2>
    </node>
</root>

【程序输出】
程序代码:
<root>
    <node>
        <key1>value1</key1>
        <key2>value2</key2>
    </node>
    <node>
        <key1>value3</key1>
        <key2>value4</key2>
    </node>
</root>
搜索更多相关主题的帖子: Java 技术 
2014-11-09 18:52
love云彩
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:青藏高原
等 级:贵宾
威 望:53
帖 子:3663
专家分:11416
注 册:2012-11-17
收藏
得分:20 
实际开发多用dom4j,各种jar包有各自的优点

思考赐予新生,时间在于定义
2014-11-09 19:03
CsRocky
Rank: 1
等 级:新手上路
帖 子:4
专家分:3
注 册:2015-4-19
收藏
得分:0 
我也看了一下dom4j,很好用
2015-04-19 20:37
快速回复:《Java 核心技术》XML 文件解析的示例程序
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019838 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved