`

JAXP中的dom解析xml

    博客分类:
  • xml
xml 
阅读更多

一.jaxp开发包对xml文件的解析,jaxp使用的解析器是IBM公司的xerces,IBM将此解析器捐献给了apache。



/**@description  xml文件*/


<?xml version="1.0" encoding="UTF-8"?><书架>

<书>

<书名 name="abc">JavaWEB</书名>

<作者>李文慧</作者>

<售价>100.00元</售价>

</书>

<书>

<书名>程序人生</书名>

<作者>小刚</作者>

<售价>28.00元</售价>

</书>

<s>aaaa</s>

</书架>



/***

 * @descriptin jaxp解析xml文档,Java自带的开发包是使用的IBM公司写的解析器xerces,但是此解析器IBM捐献                              给Apache

 * @time 2012-10-30 17:02:00

 * @author Roinli

 */

public class Demo1 {

public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, TransformerException {

// 创建工厂对象

DocumentBuilderFactory newInstance = DocumentBuilderFactory.newInstance();

// 通过工厂获得解析器

DocumentBuilder builder = newInstance.newDocumentBuilder();

System.out.println(builder);

// 对xml文档解析

Document document = builder.parse(new File("src/cn//aisino//jaxp//book.xml"));

//findNode(document);// 查找节点文本值

//findNodeAtrr(document); // 查找属性值

//addNode(document);

//addNode1(document);

//deleteNode(document);

//deleteNode1(document);

//editNode1(document);

//editNodeAttr(document);

getALLNode(document.getElementsByTagName("书").item(0));

}

// 对文档增删改查

// 查找 <书名>Java就业培训教程</书名>

public static void findNode(Document document){

NodeList nodeList =document.getElementsByTagName("书名");

Node node = nodeList.item(0);

System.out.println(node.getTextContent());

}

// 查找属性<书名 name="abc">Java就业培训教程</书名>

public static void findNodeAtrr(Document document){

Node node =document.getElementsByTagName("书名").item(0);

if(node.getNodeType() == node.ELEMENT_NODE){

Element element = (org.w3c.dom.Element)node;

String attrvalue = element.getAttribute("name");

System.out.println(attrvalue);

}

}

// 增加节点,在后面追加

public static void addNode(Document document) throws TransformerException{

Node price = document.createElement("售价");

price.setTextContent("59元");

Node book = document.getElementsByTagName("书").item(0);

book.appendChild(price);

// 更新内存中document对象写入到xml文档

TransformerFactory transformer = TransformerFactory.newInstance(); //Transaction

Transformer newTransformer = transformer.newTransformer();

// 将dom中的数据转换到book.xml文件中

newTransformer.transform(new DOMSource(document),new StreamResult(new File("src/cn//aisino//jaxp//book.xml")));

}

// 增加节点 在指定位置添加节点

public static void addNode1(Document document) throws TransformerException{

Node price = document.createElement("售价");

price.setTextContent("59元");

Node book = document.getElementsByTagName("书").item(0);

// 增加节点 在指定位置添加节点

book.insertBefore(price, document.getElementsByTagName("售价").item(0));

// 更新内存中document对象写入到xml文档

TransformerFactory transformer = TransformerFactory.newInstance(); //Transaction

Transformer newTransformer = transformer.newTransformer();

// 将dom中的数据转换到book.xml文件中

newTransformer.transform(new DOMSource(document),new StreamResult(new File("src/cn//aisino//jaxp//book.xml")));

}

// 删除节点 

public static void deleteNode(Document document) throws TransformerException{

Node book = document.getElementsByTagName("书").item(0);

book.removeChild(document.getElementsByTagName("售价").item(0));

// 更新内存中document对象写入到xml文档

TransformerFactory transformer = TransformerFactory.newInstance(); //Transaction

Transformer newTransformer = transformer.newTransformer();

// 将dom中的数据转换到book.xml文件中

newTransformer.transform(new DOMSource(document),new StreamResult(new File("src/cn//aisino//jaxp//book.xml")));

}

// 删除节点 优雅方式

public static void deleteNode1(Document document) throws TransformerException{

Node price = document.getElementsByTagName("售价").item(0);

price.getParentNode().removeChild(price);;

// 更新内存中document对象

TransformerFactory transformer = TransformerFactory.newInstance(); //Transaction

Transformer newTransformer = transformer.newTransformer();

// 将dom中的数据转换到book.xml文件中

newTransformer.transform(new DOMSource(document),new StreamResult(new File("src/cn//aisino//jaxp//book.xml")));

}

// 修改节点 将书价格节点值改成2

public static void editNode1(Document document) throws TransformerException{

Node price = document.getElementsByTagName("售价").item(0);

price.setTextContent("100.00元");

// 更新内存中document对象

TransformerFactory transformer = TransformerFactory.newInstance(); //Transaction

Transformer newTransformer = transformer.newTransformer();

// 将dom中的数据转换到book.xml文件中

newTransformer.transform(new DOMSource(document),new StreamResult(new File("src/cn//aisino//jaxp//book.xml")));

}

// 修改节点 属性值

public static void editNodeAttr(Document document) throws TransformerException{

Node book = document.getElementsByTagName("书名").item(0);

if(book.getNodeType() == book.ELEMENT_NODE){

Element element = (org.w3c.dom.Element)book;

Attr attributeNode = element.getAttributeNode("name");

attributeNode.setValue("yyyyy");

}

// 更新内存中document对象

TransformerFactory transformer = TransformerFactory.newInstance(); //Transaction

Transformer newTransformer = transformer.newTransformer();

// 将dom中的数据转换到book.xml文件中

newTransformer.transform(new DOMSource(document),new StreamResult(new File("src/cn//aisino//jaxp//book.xml")));

}

// 遍历xml文档的所有内容  "递归"

public static void getALLNode(Node node){

if(node.getNodeType() == Node.ELEMENT_NODE){

//System.out.println(node.getNodeName());

String content = node.getTextContent().trim();

if(!"".equals(content)){

System.out.println(content.trim());

}

NodeList childNodes = node.getChildNodes();

for(int i=0;i < childNodes.getLength();i++){

Node item = childNodes.item(i);

getALLNode(item);

}

}

}

}

 

分享到:
评论

相关推荐

    经典的DOM解析XML范例

    用Java解析XML文档,最常用的有两种方法:使用基于事件的XML简单API(Simple API for XML)称为SAX和基于树和节点的文档对象模型(Document Object Module)称为DOM。Sun公司提供了Java API for XML Parsing(JAXP)...

    Dom4j 解析XML

    与利用DOM、SAX、JAXP机制来解析xml相比,DOM4J 表现更优秀,具有性能优异、功能强大和极端易用使用的特点,只要懂得DOM基本概念,就可以通过dom4j的api文档来解析xml。dom4j是一套开源的api。实际项目中,往往选择...

    开源XML解析包dom4j

    一个很好的用来解析XML文件的解析包 DOM4J是dom4j.org出品的一个开源XML解析包,它的网站中这样定义: Dom4j is an easy to use, open source library for working with XML, XPath and XSLT on the Java platform ...

    XML的Jaxp解析示例和简单项目

    XML有3中解析方式:Jaxp、Jdom和dom4j 这个是sun公司提供的简单的Jaxp解析方式。

    dom4j---xml解析jar包

    dom4j是一个简单的开源库,用于处理XML、 XPath和XSLT,它基于Java平台,使用Java的集合框架,全面集成了DOM,SAX和JAXP。

    使用jaxp开发包中的SAX方式解析xml文档

    day02_xml *demo01.App1\App2\App3 使用jaxp开发包中的SAX方式解析xml文档,该方式只能读取文档 *demo4j01.App dom4j CURD XML *domo4j.ex 对dom4j 的封装例子

    java解析xml及4种常用解析比较

    JDOM还包括对程序行为的相当广泛检查以防止用户做任何在XML中无意义的事。然而,它仍需要您充分理解XML以便做一些超出基本的工作(或者甚至理解某些情况下的错误)。这也许是比学习DOM或JDOM接口都更有意义的工作。

    java_Dom4j解析XML详解

    DOM4J是 dom4j.org 出品的一个开源 XML 解析包。DOM4J应用于 Java 平台,采用了 Java 集合框架并完全支持 DOM,SAX 和 JAXP。  DOM4J 使用起来非常简单。只要你了解基本的 XML-DOM 模型,就能使用。  Dom:把整个...

    DOM4J使用简介(很实用)

    DOM4J是dom4j.org出品的一个开源XML解析包,它的网站中这样定义: Dom4j is an easy to use, open source library for working with XML, XPath and XSLT on the Java platform using the Java Collections ...

    Dom4j_使用简介

    Dom4j_使用简介DOM4J是dom4j.org出品的一个开源XML解析包,它的网站中这样定义: Dom4j is an easy to use, open source library for working with XML, XPath and XSLT on the Java platform using the Java ...

    使用dom4j操作xml

    Dom4j是一个开源的Java XML 解析处理工具,用来读写XML文件,它应用于Java平台,采用了Java集合框架并完全支持DOM, SAX和JAXP。...可以作为解析XML文档析首先API。本文讲述了如何使用Dom4j来进行XML解析。

    Dom4j是一个易用的、开源的库

    DOM4J是dom4j.org出品的一个开源XML解析包,它的网站中这样定义: Dom4j is an easy to use, open source library for working with XML, XPath and XSLT on the Java platform using the Java Collections ...

    XML_dom_sax_dom4j:XML解析技术之dom、Sax、dom4j以及XML Schema技术约束

    XML_StuGradeExam_domXML解析技术之dom、Sax、dom4j技术演习以及Schema XML文档约束##XML解析开发包Jaxp(sun)Jdomdom4j####ExamExam是一个使用Xml持久化保存数据的xml数据库,该项目是演练dom解析XML技术很好的一个...

    Dom4j 使用指南

    DOM4J是dom4j.org出品的一个开源XML解析包,它的网站中这样定义: Dom4j is an easy to use, open source library for working with XML, XPath and XSLT on the Java platform using the Java Collections ...

    4种常见的xml解析方法

    DOM(JAXP Crimson解析器) DOM是用与平台和语言无关的方式表示XML文档的官方W3C标准。DOM是以 层次结构组织的节点或信息片断的集合。

    Android 解析XML 文件的四种方法总结

    java解析xml文件四种方式 1.介绍 1)DOM(JAXP Crimson解析器) DOM是用与平台和语言无关的方式表示XML文档的官方W3C标准。DOM是以层次结构组织的节点或信息片断的集合。这个层次结构允许开发人员在树中寻找特定...

    疯狂xml讲义

    其中的DOM、SAX、JAXP、dom4j和JDOM都以结构化的方式来创建、解析XML文档,从而可以将XML文档作为数据传输工具,而XQuery则是一种新规范,通过XQuery可以查询XML文档中的数据,就像使用SQL查询关系数据库的数据一样...

    Android 创建与解析XML(五)——详解Dom4j方式

    dom4j is an easy to use, open source library for working with XML, XPath and XSLT on the Java platform using the Java Collections Framework and with full support for DOM, SAX and JAXP. dom4j官方网址:...

    疯狂XML讲义(Web Service).pdf

    其中的DOM、SAX、JAXP、dom4j和JDOM都以结构化的方式来创建、解析XML文档,从而可以将XML文档作为数据传输工具,而XQuery则是一种新规范,通过XQuery可以查询XML文档中的数据,就像使用SQL查询关系数据库的数据一样...

Global site tag (gtag.js) - Google Analytics