java DOM解析XML(1)
===========================读取XML文件的数据========================================
XML源文件
<?xml version="1.0"?>
<people>
<person personid="ACCP01">
<name>li</name>
<age>42</age>
<address>shashi</address>
</person>
<person personid="ACCP02">
<name>liu</name>
<age>23</age>
<address>wuhan</address>
</person>
<person personid="ACCP03">
<name>wang</name>
<age>25</age>
<address>wuhan</address>
</person>
</people>
DOM枚举上面XML文档的数据:
import org.w3c.dom.*;
import javax.xml.parsers.*;
public class Untitled2 {
public static void main(String args[]) throws Exception
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse("liu.xml");
//可以为绝对路径 也可以为相对路径 为相对路径时 在工程中加入XML文件
NodeList n1 = doc.getElementsByTagName("person");
for (int i = 0; i < n1.getLength(); i++) {
Element node = (Element) n1.item(i);
System.out.print("name:");
System.out.println(node.getElementsByTagName("name").item(0).
getFirstChild().getNodeValue());
System.out.print("age:");
System.out.println(node.getElementsByTagName("age").item(0).
getFirstChild().getNodeValue());
System.out.print("address:");
System.out.println(node.getElementsByTagName("address").item(0).
getFirstChild().getNodeValue());
System.out.println();
}
}
}
============================创建XML文档并用树形方式显示在控制台上===========================
import org.w3c.dom.*;
import org.apache.crimson.tree.*;
import javax.xml.parsers.*;
public class Untitled2 {
public static void main(String args[]) throws Exception
{
Element peop=null;
Element per=null;
//Element per1=null;
Element na=null;
// Element na1=null;
Element ag=null;
// Element ag1=null;
Element address=null;
//Element address1=null;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.newDocument();
if(doc!=null)
{
peop=doc.createElement("PEOPLE");
per=doc.createElement("PERSON");
// per1=doc.createElement("PERSON");
peop.appendChild(per);
// peop.appendChild(per1);
na=doc.createElement("NAME");
na.appendChild(doc.createTextNode("li"));
per.appendChild(na);
ag=doc.createElement("age");
ag.appendChild(doc.createTextNode("42"));
per.appendChild(ag);
address=doc.createElement("ADDRESS");
address.appendChild(doc.createTextNode("shashi"));
per.appendChild(address);
// na1=doc.createElement("NAME");
// na1.appendChild(doc.createTextNode("liu"));
// per1.appendChild(na1);
// ag1=doc.createElement("age");
// ag1.appendChild(doc.createTextNode("23"));
// per1.appendChild(ag1);
// address1=doc.createElement("ADDRESS");
// address1.appendChild(doc.createTextNode("shashi"));
// per1.appendChild(address1);
doc.appendChild(peop);
((XmlDocument)doc).write(System.out);
}
}
}
=============读取XML文件==========================
---》XML文档如下
<?xml version="1.0"?>
<people>
<person>
<name>li</name>
<age>42</age>
<address>shashi</address>
</person>
<person>
<name>liu</name>
<age>23</age>
<address>wuhan</address>
</person>
<person>
<name>wang</name>
<age>25</age>
<address>wuhan</address>
</person>
</people>
----》源代码如下
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.*;
class B{
public static void main(String args[]) throws Exception
{
DocumentBuilderFactory DBF=DocumentBuilderFactory.newInstance();
DocumentBuilder DB=DBF.newDocumentBuilder();
Document DOC=DB.parse("A.xml");
NodeList N1=DOC.getElementsByTagName("person");
for(int i=0;i<N1.getLength();i++)
{
Element E=(Element)N1.item(i);
NodeList NL=E.getElementsByTagName("address");
E.removeChild(NL.item(0));
}
TransformerFactory TF=TransformerFactory.newInstance();
Transformer T=TF.newTransformer();
DOMSource DOMS=new DOMSource(DOC);
StreamResult SR=new StreamResult(System.out);
T.transform(DOMS,SR);
}
}
==========================================删除XML数据============
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.*;
class Untitled1 {
public static void main(String args[]) throws Exception
{
DocumentBuilderFactory DBF=DocumentBuilderFactory.newInstance();
DocumentBuilder DB=DBF.newDocumentBuilder();
Document DOC=DB.parse("xml1.xml");
NodeList N1=DOC.getElementsByTagName("person");
for(int i=0;i<N1.getLength();i++)
{
Element E=(Element)N1.item(i);
NodeList NL=E.getElementsByTagName("address");
E.removeChild(NL.item(0)); //删除
}
TransformerFactory TF=TransformerFactory.newInstance();
Transformer T=TF.newTransformer();
DOMSource DOMS=new DOMSource(DOC);
StreamResult SR=new StreamResult(System.out);
T.transform(DOMS,SR);
}
}
=========================JDBC将XML数据插入到数据库==========================================
---------------------------首先创建表:
create table people(
name1 varchar(20),
age varchar(10),
address varchar(50)
)
--------------------------accp.xml源文件如下:
<?xml version="1.0"?>
<people>
<person>
<name>li</name>
<age>42</age>
<address>shashi</address>
</person>
<person>
<name>liu</name>
<age>23</age>
<address>wuhan</address>
</person>
<person>
<name>wang</name>
<age>25</age>
<address>wuhan</address>
</person>
</people>
--------------------------源代码如下:
import java.sql.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
public class Untitled1 {
public static void main(String args[]) throws Exception
{
Connection conn=null;
Statement stmt=null;
String na,ag,add,sql;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn=DriverManager.getConnection("jdbc:odbc:qq","sa","");
stmt=conn.createStatement();
DocumentBuilderFactory DBF=DocumentBuilderFactory.newInstance();
DocumentBuilder DB=DBF.newDocumentBuilder();
Document DOC=DB.parse("accp.xml");
NodeList N1=DOC.getElementsByTagName("person");
for(int i=0;i<N1.getLength();i++)
{
Element E=(Element)N1.item(i);
na=E.getElementsByTagName("name").item(0).getFirstChild().getNodeValue();
ag=E.getElementsByTagName("age").item(0).getFirstChild().getNodeValue();
add=E.getElementsByTagName("address").item(0).getFirstChild().getNodeValue();
sql="insert into people values(\'"+na+"\',\'"+ag+"\',\'"+add+"\')";
int ret=stmt.executeUpdate(sql);
}
System.out.println("已经将XML数据插入到数据库中");
stmt.close();
conn.close();
}
}
===========如何从数据库表中取出数据并以树形方式在消息窗口显示:============
import org.apache.crimson.tree.*;
import org.w3c.dom.*;
import java.sql.*;
import javax.xml.parsers.*;
import java.io.*;
public class Untitled1 {
public static void main(String args[]) throws Exception
{
Element peop=null;
Element per=null;
Element na=null;
Element ag=null;
Element add=null;
Connection conn=null;
ResultSet rs=null;
Statement stmt=null;
Document doc=null;
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn=DriverManager.getConnection("jdbc:odbc:mydata","sa","");
stmt=conn.createStatement();
rs=stmt.executeQuery("select * from people");
DocumentBuilderFactory DBF=DocumentBuilderFactory.newInstance();
DocumentBuilder DB=DBF.newDocumentBuilder();
doc=DB.newDocument();
peop=doc.createElement("people");
while(rs.next())
{
per=doc.createElement("person");
peop.appendChild(per);
na=doc.createElement("name");
na.appendChild(doc.createTextNode(rs.getString("name1")));
per.appendChild(na);
ag=doc.createElement("age");
ag.appendChild(doc.createTextNode(rs.getString("age")));
per.appendChild(ag);
add=doc.createElement("address");
add.appendChild(doc.createTextNode(rs.getString("address")));
per.appendChild(add);
}
doc.appendChild(peop);
((XmlDocument)doc).write(System.out);
}
}