吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 3106|回复: 8
收起左侧

[Java 转载] DOM解析XML

[复制链接]
jzwlove 发表于 2016-12-29 01:35
本帖最后由 jzwlove 于 2016-12-29 01:46 编辑

解析一个book.xml内容为
[XML] 纯文本查看 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<books>
        <book id="0001">
                <title>水浒传</title>
                <price>1220.58</price>
        </book>
        <book id="0002">
                <title>西游记</title>
                <price>2128.58</price>
        </book>
</books>


创建的Book的javaBean类
[Java] 纯文本查看 复制代码
package cn.java9.beans;

public class Book {
        private String id;
        private String title;
        private double price;

        public String getId() {
                return id;
        }

        public void setId(String id) {
                this.id = id;
        }

        public String getTitle() {
                return title;
        }

        public void setTitle(String title) {
                this.title = title;
        }

        public double getPrice() {
                return price;
        }

        public void setPrice(double price) {
                this.price = price;
        }

        public Book(String id, String title, double price) {
                this.id = id;
                this.title = title;
                this.price = price;
        }

        public Book() {

        }

        @Override
        public String toString() {
                return "Book [id=" + id + ", title=" + title + ", price=" + price + "]";
        }

}

解析开始
[Java] 纯文本查看 复制代码
package cn.java9.xml;

import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import cn.java9.beans.Book;

public class DomDemo01 {
        public static void main(String[] args) throws Exception {

                getAllBook();

        }

        public static List<Book> getAllBook() throws Exception {
                List<Book> bookList = new ArrayList<Book>();

                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                DocumentBuilder documentBuilder = factory.newDocumentBuilder();
                Document doc = documentBuilder.parse("books.xml");
                NodeList nodes = doc.getElementsByTagName("book");
                for (int i = 0; i < nodes.getLength(); i++) {
                        Book book = new Book();
                        Node node = nodes.item(i);
                        Element ele = (Element) node;
                        String id = ele.getAttribute("id");
                        book.setId(id);

                        NodeList childNodes = node.getChildNodes();
                        for (int j = 0; j < childNodes.getLength(); j++) {
                                Node childNode = childNodes.item(j);
                                String nodeName = childNode.getNodeName();
                                if ("title".equals(nodeName)) {
                                        String title = childNode.getTextContent();
                                        book.setTitle(title);

                                }
                                if ("price".equals(nodeName)) {
                                        double price = Double.parseDouble(childNode
                                                        .getTextContent());
                                        book.setPrice(price);

                                }

                        }
                        bookList.add(book);
                }

                System.out.println(bookList);
                return bookList;
        }
}

新建位图图像.jpg

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

psx1lin 发表于 2016-12-29 08:27
收藏
參考一下
3q
haking 发表于 2016-12-29 09:26
一般这种东西我都会用正则来做,代码量一下子少了N多。。。
头像被屏蔽
屌絲钕 发表于 2016-12-29 09:31
jackfang 发表于 2016-12-29 10:57
谢谢分享!!!
cqyisbug 发表于 2017-1-2 12:52
解析xml我一般用xstream,xml转obj  或者obj转xml都可以,比较方便.
52gta 发表于 2017-1-4 20:37
DOM解析大数据不行啊===
messyidea 发表于 2017-1-5 17:04
这类工作我一般喜欢用python写,哈哈~
A.Thousand.Suns 发表于 2017-1-18 17:08
推荐学习,但是我个人是最不喜欢DOM的,不是麻烦,而是API很怪
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-3-28 20:34

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表