XML解析JDom和JDOM(SAX方式)
来源:互联网 发布:js点击下一页 编辑:程序博客网 时间:2024/09/21 09:04
http://jdom.org/dist/binary/下载JDom
student.xml
import org.jdom.input.*;import org.jdom.Attribute;import org.jdom.Element;import org.xml.sax.SAXException;import java.io.*;import java.util.List;import org.apache.xerces.parsers.*;public class JDomDOM { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub DOMBuilder builder = new DOMBuilder(); DOMParser parser = new DOMParser(); String path = "D:\\student.xml"; String output = ""; try { parser.parse(path); org.w3c.dom.Document domDocument = parser.getDocument(); org.jdom.Document jdomDocument = builder.build(domDocument); Element root = jdomDocument.getRootElement(); output += "This XML document's root node is: " + root.getName() + "\r\n"; List <Element> children = root.getChildren(); output += "The root has " + children.size() + " subNodes \r\n"; for(int i = 0; i < children.size(); i++){ Element node = children.get(i); output += "In the " + i + 1 + node.getName() + "subNode: \r\n"; List <Attribute> attrs = node.getAttributes(); for(int k = 0; k < attrs.size(); k++){ Attribute attr = attrs.get(k); output += "The " + k + 1 + "attribute is " + attr.getName(); output += " the value is: " + attr.getValue() + "\r\n"; } List <Element> childrenList = node.getChildren(); for(int j = 0; j < childrenList.size(); j++){ Element childNode = childrenList.get(j); output += "The " + j + 1 + "subNode is: " + childNode.getName(); output += " value is " + childNode.getValue() + "\r\n"; } } System.out.println(output); } catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}
JDom解析XML(SAX方式)
import org.jdom.input.*;import org.jdom.*;import java.io.*;import java.util.List;import org.apache.xerces.parsers.*;public class JDomSAX { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub SAXBuilder builder = new SAXBuilder(); String path = "D:\\student.xml"; String output = ""; Document doc; try { doc = builder.build(path); Element root = doc.getRootElement(); List <Element>children = root.getChildren(); output += "sno\t" + "name\t" + "age\t" + "course\t" + "\r\n"; for(int i = 0; i < children.size(); i++){ Element node = children.get(i); Attribute attr = node.getAttribute("sno"); output += attr.getValue() + "\t"; output += node.getChildText("name") + "\t"; output += node.getChildText("age") + "\t"; output += node.getChildText("course") + "\r\n"; } System.out.println(output); } catch (JDOMException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}
student.xml
<?xml version="1.0" encoding="gb2312"?><students> <student sno="110"> <name>Mark</name> <age>23</age> <course>English</course> </student> <student sno="114"> <name>Andy</name> <age>19</age> <course>Chinese</course> </student></students>
- XML解析JDom和JDOM(SAX方式)
- sax,jdom解析xml
- XML解析(DOM SAX JDOM DOM4J)
- Java解析XML:Jdom解析和SAX解析
- xml的解析方式——dom/sax、jdom、dom4j
- XML解析方式之DOM、SAX、JDOM、DOM4J
- XML解析三种方式JDom、Sax、Dom4j
- XML解析方式之DOM、SAX、JDOM、DOM4J
- DOM,SAX,JDOM,DOM4J 四种方式解析xml
- DOM,SAX,JDOM,DOM4J 四种方式解析xml
- 解析xml的四种方式:DOM JDOM DOM4J SAX
- xml---4种解析方式dom,sax,jdom,dom4j
- XML--jdom/dom4j/sax解析XML文件
- android解析XML总结(SAX、Pull、Dom三种方式)附带DOM4J、JDOM
- android解析XML总结(SAX、Pull、Dom三种方式)附带DOM4J、JDOM
- Dom、Dom4J、Sax、JDom、读取(解析)Xml
- xml解析技术介绍(dom4j,dom,jdom,sax)
- Java解析XML汇总(DOM/SAX/JDOM/DOM4j/XPath)
- 刷陈题ing @@
- 《iPhone与iPad开发实战——iOS 经典应用剖析》出版在即,敬请期待
- 三层架构-最简洁的案例
- what is PVOID ??
- Redis实现MongoDB的getlasterror功能
- XML解析JDom和JDOM(SAX方式)
- Android 4.0 的发布了
- 框架页中Silverlight和Web页面之间跳转和返回
- QQ交谈的js
- Java图形用户界面3—AWT事件模型
- jQuery学习笔记--JqGrid相关操作 方法列表 备忘 重点讲解(超重要)
- jquery-autocomplete的用法及参数
- Redis-Sampler:深入了解你的Redis存储
- 可综合的verilog语法