java中使用xml
来源:互联网 发布:桔子网络理财可靠吗 编辑:程序博客网 时间:2024/06/14 20:03
//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<books.size();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
<?xml version="1.0" encoding="gb2312"?>
<!DOCTYPE abc [
<!ELEMENT note (男|女)>
]>
<abc>
<note>男</note>
</abc>
abc.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE booklist>
<booklist>
<book>
<code>1</code>
<title>XML讲解</title>
<author>张三</author>
<price>100</price>
</book>
<book>
<code>2</code>
<title>HTML讲解</title>
<author>李四</author>
<price>100</price>
</book>
<book>
<code>3</code>
<title>JSP讲解</title>
<author>王五</author>
<price>100</price>
</book>
</booklist>
testDocument.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE booklist>
<booklist>
<book>
<code>1</code>
<title>XML讲解</title>
<author>张三</author>
<price>100</price>
</book>
<book>
<code>2</code>
<title>HTML讲解</title>
<author>李四</author>
<price>100</price>
</book>
<book>
<code>3</code>
<title>JSP讲解</title>
<author>王五</author>
<price>100</price>
</book>
</booklist>
xmltest.dtd
<?xml version="1.0" encoding="UTF-8"?>
<!ELEMENT booklist (book)+>
<!ELEMENT book (code,title,author,price)>
<!ELEMENT code (#PCDATA)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT price (#PCDATA)>
<!ATTLIST book id ID #REQUIRED
type (IT|科普) "科普">
- java中使用xml
- java中使用xml二
- java 中xml的使用
- JAVA中使用Schema校验XML
- java中使用xsd验证xml
- JAVA中使用Schema校验XML
- java中使用JAXP解析xml
- java中使用schema验证xml文档
- JAVA中使用DOM操作XML
- JAVA中使用dom4j操作XML
- java中使用SAX解析XML
- 在java中使用sax解析xml
- JAVA中使用Schema校验XML
- 在java中使用dom4j解析xml
- Java中使用DOM解析XML
- Java 中使用 SAX 解析 XML 文档
- java中使用SAX生成XML文件
- java中使用dom解析xml
- C#多线程异步访问winform中控件
- 最简单方法搞定 VirtualBox 虚拟机共享文件问题
- Android Notification 显示后消失
- 备忘日志【三】
- 第九周任务一
- java中使用xml
- SessionFactory
- 转:Linux内核模块基础
- 深入浅出Windows BATCH
- 第九周二任务5:设计一元一次方程类,求形如ax+b=0的方程的解
- linux网络编程之:接受网络中的ARP数据并分析(附C语言实现)
- Hibernate操作MySQL视图
- 第九周C++ 任务五。设计一元一次方程类,求形如ax+b=0的方程的解。
- 2012.4.16-新的开始