JSOUP 解析XML文件
来源:互联网 发布:晋中市网络平台登录 编辑:程序博客网 时间:2024/06/07 05:14
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>
0 0
- JSOUP 解析XML文件
- 利用jsoup解析网页和xml文件
- Jsoup解析HTML,XML
- Jsoup解析网络xml
- java解析XML--jsoup
- Jsoup解析HTML文件
- Jsoup解析文件
- 使用Jsoup解析html文件
- 使用JSoup解析HTML文件
- jsoup解析html文件实例
- XML解析神奇比较Jsoup or Xpath
- 使用Jsoup简单解析HTML文件示例
- jsoup解析文件存入本地mongodb数据库
- 网络爬虫--数据处理,jsoup工具解析html,dom4j解析xml
- jsoup解析
- 使用Jsoup解析XML抓取新浪新闻文章
- xml解析神器PK,花落谁家?Jsoup Or Xpath?
- html&xml解析之jsoup和domj4的简单使用
- JavaScript动态规划实现最长公共子序列
- hdu 1097 A hard puzzle (快速幂)
- 别人家的技术leader是如何建设团队、管理人员、沟通工作的?
- java 集合精华一页纸
- 图解字符串的朴素模式匹配算法
- JSOUP 解析XML文件
- MFC单文档之利用全局变量一次设置多个对话框的串口通讯
- Visual Studio Code Extension 推荐
- 字符串和字符串的常见存储结构
- springmvc 拦截器
- 洛谷 P1447 能量采集
- Mysql中的高级运算
- 函数与函数使用
- kubeadm工作机制分析