java中xml经常使用吗_java中使用xml

该博客展示了如何使用DOM和JDOM两种方式解析XML文件。DOM方式通过DocumentBuilderFactory和DocumentBuilder创建并解析XML文档,遍历元素节点。JDOM方式则利用SAXBuilder构建Document对象,并获取特定元素的子节点进行操作,还演示了如何修改和输出XML内容。
摘要由CSDN通过智能技术生成

//DOM方式

package com.neusoft.xml;

import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;

import org.w3c.dom.Node;

import org.w3c.dom.NodeList;

import org.xml.sax.SAXException;

public class DomXmlSample {

/**

* @param args

* @throws ParserConfigurationException

* @throws IOException

* @throws SAXException

*/

public static void main(String[] args) throws ParserConfigurationException,

SAXException, IOException {

// TODO Auto-generated method stub

// 1.获得一个新DocumentBuilderFactory实例(定义工厂API,使应用程序能够从 XML 文档获取生成 DOM

// 对象树的解析器)

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

// 2.使用DocumentBuilderFactory构建DocumentBuilder(定义 API, 使其从 XML 文档获取 DOM

// 文档实例。使用此类,应用程序员可以从 XML 获取一个 Document)

DocumentBuilder db = dbf.newDocumentBuilder();

// 3.使用DocumentBuilder的parse()方法解析文件

// 4.将已解析的文档存储在Document对象中

Document document = db.parse("testDocument.xml");

// 5.使用getElementsByTagName()方法获得元素

NodeList bookList = document.getElementsByTagName("book");

// 获取所有的子节点

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

System.out.println("========第" + i + "记录============");

Node node = bookList.item(i);

NodeList childNodes = node.getChildNodes();

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

Node childN = childNodes.item(j);

if (childN.getFirstChild() != null) {

System.out.print(childN.getNodeName());

System.out.println(":"

+ childN.getFirstChild().getNodeValue());

}

}

}

}

}

package com.neusoft.xml;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.List;

import org.jdom.Document;

import org.jdom.Element;

import org.jdom.JDOMException;

import org.jdom.input.SAXBuilder;

import org.jdom.output.XMLOutputter;

public class JdomXmlSample {

/**

* @param args

* @throws IOException

* @throws JDOMException

*/

public static void main(String[] args) throws JDOMException, IOException {

// TODO Auto-generated method stub

SAXBuilder builder=new SAXBuilder(false);//表示使用默认的解析器

Document document=builder.build("testDocument.xml");

Element root=document.getRootElement();

List books=root.getChildren("book");

for(int i=0;i

{

Element book=(Element)books.get(i);

Element code=book.getChild("code");

Element title=book.getChild("title");

Element author=book.getChild("author");

Element price=book.getChild("price");

System.out.println("code="+code.getText()+"\t"+"title="+title.getText()

+"\tauthor="+author.getText()+"\t"+"price="+price.getText());

// System.out.println("code:"+book.getChildText("code")+

//              "\ttitle:"+book.getChildText("title")

//              +"\tauthor:"+book.getChildText("author")+

//              "\tprice:"+book.getChildText("price"));

book.getChild("price").setText("100");

}

XMLOutputter  outputter=new XMLOutputter();

outputter.output(document, new FileOutputStream("abc.xml"));

}

}

a.xml

]>

abc.xml

1

XML讲解

张三

100

2

HTML讲解

李四

100

3

JSP讲解

王五

100

testDocument.xml

1

XML讲解

张三

100

2

HTML讲解

李四

100

3

JSP讲解

王五

100

xmltest.dtd

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值