JSOUP 解析XML文件

Jsoup 可以用于解析、操作HTML,同样也可以用来解析XML,这里只简单介绍一下,更多功能可以查看源码API

package com.hk.internethospital.justtest;

import org.jsoup.Jsoup;
import org.jsoup.nodes.*;
import org.jsoup.select.Elements;

import java.io.BufferedReader;
import java.io.FileReader;

/**
 * 利用JSOUP解析XML文件
 * Created by libingxian on 2017/4/12.
 */
public class JsoupXmlDemo {

    /**
     * 从指定文件中读取xml内容
     * @param xmlFilePath
     * @return
     * @throws Exception
     */
    public String getXmlStringFromFile(String xmlFilePath) throws Exception{
        StringBuilder stringBuilder = new StringBuilder();
        FileReader fileReader = new FileReader(xmlFilePath);
        BufferedReader bufferedReader= new BufferedReader(fileReader);
        String line;
        while ((line = bufferedReader.readLine())!= null){
            stringBuilder.append(line);
        }
        return stringBuilder.toString();
    }

    /**
     * 从指定的xml内容中读取对应的元素
     * @param xmlStr
     */
    public void getXmlElementValue(String xmlStr){
        Document document = Jsoup.parse(xmlStr);
        Elements elements = document.select("recipe recipename ");
        Node node = new DataNode("aaaaa","<asdf>");
        for (Element em:elements) {
//            em.after(node);
            System.out.print("节点名称:" + em.nodeName() + " \n");
            Attributes attributes = em.attributes();
            for (Attribute at:attributes) {
                System.out.print(" -节点属性:" + at + "\n") ;
            }
            System.out.print(" -节点文本:" + em.text());
            System.out.print("\n");
        }

    }

    public static void main(String args[]) throws Exception{
        String xmlPath = "";
        JsoupXmlDemo jxd = new JsoupXmlDemo();
        jxd.getXmlElementValue(jxd.getXmlStringFromFile(System.getProperty("user.dir") + "jsoupxmldemo.xml"));
    }

}

xml 内容

<?xml version="1.0" encoding="UTF-8"?>
<recipe type="dessert">
    <recipename cuisine="american" servings="1">Ice Cream Sundae</recipename>
    <ingredlist>
        <listitem><quantity units="cups">0.5</quantity>
            <itemdescription>vanilla ice cream</itemdescription></listitem>
        <listitem><quantity units="tablespoons">3</quantity>
            <itemdescription>chocolate syrup or chocolate fudge</itemdescription></listitem>
        <listitem><quantity units="tablespoons">1</quantity>
            <itemdescription>nuts</itemdescription></listitem>
        <listitem><quantity units="each">1</quantity>
            <itemdescription>cherry</itemdescription></listitem>
    </ingredlist>
    <utensils>
        <listitem><quantity units="each">1</quantity>
            <utensilname>bowl</utensilname></listitem>
        <listitem><quantity units="each">1</quantity>
            <utensilname>spoons</utensilname></listitem>
        <listitem><quantity units="each">1</quantity>
            <utensilname>ice cream scoop</utensilname></listitem>
    </utensils>
    <directions>
        <step>Using ice cream scoop, place vanilla ice cream into bowl.</step>
        <step>Drizzle chocolate syrup or chocolate fudge over the ice cream.</step>
        <step>Sprinkle nuts over the mound of chocolate and ice cream.</step>
        <step>Place cherry on top of mound with stem pointing upward.</step>
        <step>Serve.</step>
    </directions>
    <variations>
        <option>Replace nuts with raisins.</option>
        <option>Use chocolate ice cream instead of vanilla ice cream.</option>
    </variations>
    <preptime>5 minutes</preptime>
</recipe>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lbxoqy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值