zoukankan      html  css  js  c++  java
  • Java解析XML简单版

    JAVA 文件

    Main:

    Reader.java
    1. import java.util.List;
    2. public class Reader {
    3. /**
    4. * @author lxh
    5. * @param args
    6. */
    7. public static void main(String[] args) {
    8. try {
    9. List<String> list = XmlReader.readXml("dailyreport");
    10. System.out.println(list);//输出列表。
    11. System.out.println(list.get(2));//输出列表内索引为2的值。
    12. } catch (Exception e) {
    13. e.printStackTrace();
    14. }
    15. }
    16. }

    XmlReader.java

    1. import java.util.*;
    2. import java.io.File;
    3. import javax.xml.parsers.*;
    4. import org.w3c.dom.*;
    5. public class XmlReader {
    6. public static List<String> readXml(String nodeName) throws Exception {
    7. DocumentBuilder db = DocumentBuilderFactory.newInstance()
    8. .newDocumentBuilder();
    9. Document document = db.parse(new File("test.xml"));// 把XML文件解析成DOCUMENT类型
    10. Element root = document.getDocumentElement();
    11. String NodeName=nodeName; //自选XML中的节点名
    12. NodeList list = root.getElementsByTagName(NodeName);// 获得page元素
    13. List<String> list2=showElem(list);
    14. return list2;
    15. }
    16. public static List<String> showElem(NodeList nl) {
    17. List<String> list = new ArrayList<String>();
    18. for (int i = 0; i < nl.getLength(); i++) {
    19. Node n = nl.item(i);// 得到父节点
    20. // 子节点
    21. NodeList childList = n.getChildNodes();
    22. for (int x = 0; x < childList.getLength(); x++) {
    23. Node childNode = childList.item(x);
    24. // 判断取出的值是否属于Element元素,目的是过滤掉
    25. if (childNode instanceof Element) {
    26. // 得到子节点的名字
    27. //String childNodeName = childNode.getNodeName();
    28. //System.out.print("节点名:" + childNodeName);
    29. // 得到子节点的值
    30. String childNodeValue = childNode.getTextContent();
    31. list.add(childNodeValue);
    32. System.out.println("节点值:" + childNodeValue);
    33. }
    34. }
    35. }
    36. return list;
    37. }
    38. }



    test.xml

    1. <?xml version="1.0" encoding="utf-8"?>
    2. <test>
    3. <project>
    4. <code>systemMessage</code>
    5. <title>标题:系统消息</title>
    6. <content>概要:您 管理的项目已天未派发新任务</content>
    7. <sendercode>system</sendercode>
    8. <sendername>发送者:系统</sendername>
    9. <receivercode>test</receivercode>
    10. <receivername>接受者:测试</receivername>
    11. <state>状态:已读</state>
    12. <desc>内容:您管理的项目已三天未派发新任务,请注意项目进度</desc>
    13. <type>类型:systemMessage</type>
    14. <sendtime>发送时间:2015-8-17</sendtime>
    15. <tocode>回复</tocode>
    16. </project>
    17. <dailyreport>
    18. <code>systemMessage</code>
    19. <title>标题:系统消息</title>
    20. <content>概要:您 已经三天未填写日报务</content>
    21. <sendercode>system</sendercode>
    22. <sendername>发送者:系统</sendername>
    23. <receivercode>test</receivercode>
    24. <receivername>接受者:测试</receivername>
    25. <state>状态:已读</state>
    26. <desc>内容:您 已经三天未填写日报务,请及时填写</desc>
    27. <type>类型:systemMessage</type>
    28. <sendtime>发送时间:2015-8-17</sendtime>
    29. <tocode>回复</tocode>
    30. </dailyreport>
    31. </test>

    输出结果:

    节点值:systemMessage
    节点值:标题:系统消息
    节点值:概要:您 已经三天未填写日报务
    节点值:system
    节点值:发送者:系统
    节点值:test
    节点值:接受者:测试
    节点值:状态:已读
    节点值:内容:您 已经三天未填写日报务
    节点值:类型:systemMessage
    节点值:发送时间:2015-8-17
    节点值:回复
    [systemMessage, 标题:系统消息, 概要:您 已经三天未填写日报务, system, 发送者:系统, test, 接受者:测试, 状态:已读, 内容:您管理的项目已三天未派发新任务,请注意项目进度, 类型:systemMessage, 发送时间:2015-8-17, 回复]
    概要:您 已经三天未填写日报务  











  • 相关阅读:
    Blob隐藏真实路径
    Vue原理笔记3
    Vue原理笔记2
    Vue双向绑定原理
    Vue原理笔记1
    MVC、MVP、MVVM
    Go语言学习之-带分割符的文件转excel
    IBMMQ之工具类
    IBMMQ之取发文件
    JAVA之我的公共部分测试调用
  • 原文地址:https://www.cnblogs.com/luoxuehuan/p/4741455.html
Copyright © 2011-2022 走看看