java对xml全面解析,增,删,改,以及将java对象重新编组为xml文件
2007-03-16 21:36
645 查看
写了一个jaxp用dom处理xml的代码,具体例外省略,自己看吧,希望对你有帮助。
如果这里的格式不习惯可以看我在csdn的博客
http://blog.csdn.net/xhinker/archive/2007/03/16/1531717.aspx
被解析的xml文件名为AnewXMLfile.xml
<?xml version="1.0"
encoding="UTF-8"?>
<!--
Document :
AnewXMLfile.xml
Created on :
2007年3月13日, 下午3:36
Author
: xhinker
-->
<customers>
<customer>
<id>001</id>
<name>xhinker</name>
<address>harbin</address>
</customer>
<customer>
<id>002</id>
<name>Car</name>
<address>Suzhou</address>
</customer>
<customer>
<id>003</id>
<name>Jimmy</name>
<address>ChengDu</address>
</customer>
<customer>
<id>004</id>
<name>Henry</name>
<address>Xi'an</address>
</customer>
</customers>
下面是用于解析这个xml文件的java代码,文件名为XMLfileReader.java
import javax.xml.parsers.*;
import org.w3c.dom.*;
import java.io.File;
import
javax.xml.transform.dom.DOMSource;
import org.xml.sax.*;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.*;
/**
* @author xhinker
*/
public class XMLfileReader {
/**
*解组xml,使之成为Document对象
*/
public
Document XMLParse(){
//建立解析器工厂
DocumentBuilderFactory
dbf=DocumentBuilderFactory.newInstance();
Document doc=null;
try {
DocumentBuilder db=dbf.newDocumentBuilder();
File file=new File("AnewXMLfile.xml");
doc=db.parse(file);//解析xml文件
} catch (Exception e) {}
return doc;
}
/**
*显示Docuument对象里的内容
*/
public void
XMLreader(Document
doc){
try {
System.out.println("haha");
NodeList nl=doc.getElementsByTagName("customer");
int len=nl.getLength();
for(int i=0;i<len;i++){
Element customer=(Element)nl.item(i);
Node id=customer.getElementsByTagName("id").item(0);
Node name=customer.getElementsByTagName("name").item(0);
Node
address=customer.getElementsByTagName("address").item(0);
String newid=id.getFirstChild().getNodeValue();
String newname=name.getFirstChild().getNodeValue();
String newAddress=address.getFirstChild().getNodeValue();
System.out.print("id\u53F7:");
System.out.println(newid);
System.out.print("\u540D\u5B57:");
System.out.println(newname);
System.out.print("\u5730\u5740");
System.out.println(newAddress);
}
} catch (Exception e) {}
}
/**
*为Document对象里添加内容
*@param doc Document类型对象
*/
public void
add(Document doc){
try {
Element customer=doc.createElement("customer");
Element id=doc.createElement("id");
Element name=doc.createElement("name");
Element address=doc.createElement("address");
Text textId=doc.createTextNode("005");
Text textName=doc.createTextNode("zhushudong");
Text textAddress=doc.createTextNode("haerbin");
id.appendChild(textId);
name.appendChild(textName);
address.appendChild(textAddress);
customer.appendChild(id);
customer.appendChild(name);
customer.appendChild(address);
Element root=doc.getDocumentElement();
root.appendChild(customer);
} catch (Exception e) {}
}
/**
*删除结点
*@param doc Document对象
*@param i 用于指示要删除结点位置
*/
public void
delete(Document doc,int i){
NodeList nl=doc.getElementsByTagName("customer");
Node nodeDel=nl.item(i);
nodeDel.getParentNode().removeChild(nodeDel);
}
/**
*改变节点的值
*@param doc 传入的对象
*@param i 用于指示要修改结点位置
*@param attr 要修改节点的标记
*@param newValue 修改后的值
*/
public void
change(Document doc,int i,String attr,String newValue){
NodeList nl=doc.getElementsByTagName("customer");
Element cha=(Element)nl.item(i);
Node nodeForCha=cha.getElementsByTagName(attr).item(0);
nodeForCha.getFirstChild().setNodeValue(newValue);
}
/**
*将java对象转化成xml文件
*@param doc 要转化的java对象
*/
public void
ObjectToXML(Document doc)throws
TransformerConfigurationException{
try {
//利用文档节点创建一个DOM输入源
DOMSource source=new DOMSource(doc);
//以newXMLfile.xml创建一个StreamResult对象
StreamResult result=new StreamResult(new
File("newXMLfile.xml"));
//创建转换器工厂对象
TransformerFactory tff=TransformerFactory.newInstance();
//创建转换器对象
Transformer tf=tff.newTransformer();
//进行转换
tf.transform(source, result);
} catch (Exception e) {}
}
public
static void main(String[]
args){
XMLfileReader xmlReader=new XMLfileReader();
Document newdoc=xmlReader.XMLParse();
System.out.println("-----display------");
xmlReader.XMLreader(newdoc);
xmlReader.add(newdoc);
System.out.println("-------add to last--------");
xmlReader.XMLreader(newdoc);
xmlReader.delete(newdoc, 0);
System.out.println("-----delete the first node-------");
xmlReader.XMLreader(newdoc);
xmlReader.change(newdoc,0,new String("name"),new
String("zhushudong2"));
System.out.println("-----change the first node-------");
xmlReader.XMLreader(newdoc);
try {
xmlReader.ObjectToXML(newdoc);
} catch (Exception e) {}
}
}
如果这里的格式不习惯可以看我在csdn的博客
http://blog.csdn.net/xhinker/archive/2007/03/16/1531717.aspx
被解析的xml文件名为AnewXMLfile.xml
<?xml version="1.0"
encoding="UTF-8"?>
<!--
Document :
AnewXMLfile.xml
Created on :
2007年3月13日, 下午3:36
Author
: xhinker
-->
<customers>
<customer>
<id>001</id>
<name>xhinker</name>
<address>harbin</address>
</customer>
<customer>
<id>002</id>
<name>Car</name>
<address>Suzhou</address>
</customer>
<customer>
<id>003</id>
<name>Jimmy</name>
<address>ChengDu</address>
</customer>
<customer>
<id>004</id>
<name>Henry</name>
<address>Xi'an</address>
</customer>
</customers>
下面是用于解析这个xml文件的java代码,文件名为XMLfileReader.java
import javax.xml.parsers.*;
import org.w3c.dom.*;
import java.io.File;
import
javax.xml.transform.dom.DOMSource;
import org.xml.sax.*;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.*;
/**
* @author xhinker
*/
public class XMLfileReader {
/**
*解组xml,使之成为Document对象
*/
public
Document XMLParse(){
//建立解析器工厂
DocumentBuilderFactory
dbf=DocumentBuilderFactory.newInstance();
Document doc=null;
try {
DocumentBuilder db=dbf.newDocumentBuilder();
File file=new File("AnewXMLfile.xml");
doc=db.parse(file);//解析xml文件
} catch (Exception e) {}
return doc;
}
/**
*显示Docuument对象里的内容
*/
public void
XMLreader(Document
doc){
try {
System.out.println("haha");
NodeList nl=doc.getElementsByTagName("customer");
int len=nl.getLength();
for(int i=0;i<len;i++){
Element customer=(Element)nl.item(i);
Node id=customer.getElementsByTagName("id").item(0);
Node name=customer.getElementsByTagName("name").item(0);
Node
address=customer.getElementsByTagName("address").item(0);
String newid=id.getFirstChild().getNodeValue();
String newname=name.getFirstChild().getNodeValue();
String newAddress=address.getFirstChild().getNodeValue();
System.out.print("id\u53F7:");
System.out.println(newid);
System.out.print("\u540D\u5B57:");
System.out.println(newname);
System.out.print("\u5730\u5740");
System.out.println(newAddress);
}
} catch (Exception e) {}
}
/**
*为Document对象里添加内容
*@param doc Document类型对象
*/
public void
add(Document doc){
try {
Element customer=doc.createElement("customer");
Element id=doc.createElement("id");
Element name=doc.createElement("name");
Element address=doc.createElement("address");
Text textId=doc.createTextNode("005");
Text textName=doc.createTextNode("zhushudong");
Text textAddress=doc.createTextNode("haerbin");
id.appendChild(textId);
name.appendChild(textName);
address.appendChild(textAddress);
customer.appendChild(id);
customer.appendChild(name);
customer.appendChild(address);
Element root=doc.getDocumentElement();
root.appendChild(customer);
} catch (Exception e) {}
}
/**
*删除结点
*@param doc Document对象
*@param i 用于指示要删除结点位置
*/
public void
delete(Document doc,int i){
NodeList nl=doc.getElementsByTagName("customer");
Node nodeDel=nl.item(i);
nodeDel.getParentNode().removeChild(nodeDel);
}
/**
*改变节点的值
*@param doc 传入的对象
*@param i 用于指示要修改结点位置
*@param attr 要修改节点的标记
*@param newValue 修改后的值
*/
public void
change(Document doc,int i,String attr,String newValue){
NodeList nl=doc.getElementsByTagName("customer");
Element cha=(Element)nl.item(i);
Node nodeForCha=cha.getElementsByTagName(attr).item(0);
nodeForCha.getFirstChild().setNodeValue(newValue);
}
/**
*将java对象转化成xml文件
*@param doc 要转化的java对象
*/
public void
ObjectToXML(Document doc)throws
TransformerConfigurationException{
try {
//利用文档节点创建一个DOM输入源
DOMSource source=new DOMSource(doc);
//以newXMLfile.xml创建一个StreamResult对象
StreamResult result=new StreamResult(new
File("newXMLfile.xml"));
//创建转换器工厂对象
TransformerFactory tff=TransformerFactory.newInstance();
//创建转换器对象
Transformer tf=tff.newTransformer();
//进行转换
tf.transform(source, result);
} catch (Exception e) {}
}
public
static void main(String[]
args){
XMLfileReader xmlReader=new XMLfileReader();
Document newdoc=xmlReader.XMLParse();
System.out.println("-----display------");
xmlReader.XMLreader(newdoc);
xmlReader.add(newdoc);
System.out.println("-------add to last--------");
xmlReader.XMLreader(newdoc);
xmlReader.delete(newdoc, 0);
System.out.println("-----delete the first node-------");
xmlReader.XMLreader(newdoc);
xmlReader.change(newdoc,0,new String("name"),new
String("zhushudong2"));
System.out.println("-----change the first node-------");
xmlReader.XMLreader(newdoc);
try {
xmlReader.ObjectToXML(newdoc);
} catch (Exception e) {}
}
}
相关文章推荐
- java对xml全面解析,增,删,改,以及将java对象重新编组为xml文件
- SAX 解析XML文件:将XML转换成Java对象
- xml之SAX解析以及数据结构存入java对象
- Java解析Xml文件—判断Xml文件的节点是否存在子节点_以及对节点下不同子节点的内容解析方式
- 序列化对象到Xml文件以及反序列话Xml文件到对象(序列化数据到8个Java原型类型)
- Java解析Xml文件—判断Xml文件的节点是否存在子节点_以及对节点下不同子节点的内容解析方式
- Java乔晓松-使用SAX,DOM,pull解析XML文件以及pull解析生成xml文件
- 利用oxygen编辑并生成xml文件,并使用JAVA的JAXB技术完成xml的解析
- android XMl 解析神奇xstream 五: 把复杂对象转换成 xml ,并写入SD卡中的xml文件
- JAXB解析xml文件转换为Java对象
- $.each遍历json对象(java将对象转化为json格式以及将json解析为普通对象)
- XML三种解析方式简介以及JAVA实现
- Java用String 截取方式解析xml文件、处理大xml文件
- Dom解析是将xml文件全部载入,组装成一颗dom树,然后通过节点以及节点之间的关系来解析xml文件,下面结合这个xml文件来进行dom解析。
- js解析xml字符串或xml文件,将其转换为xml对象方法
- Java对象锁和类锁全面解析(多线程synchronized关键字)
- xml,json,map,java对象互相转换解析
- java对xml进行的DOM 解析以及如何对网站数据进行加密
- Java乔晓松-jQuery中的事件和解析xml文件以及Aptana插件的安装
- java xstream中的xml转对象时解析有属性和值的问题