DOM解析XML文档(二)
example3.java
import org.w3c.dom.*;
import javax.xml.parsers.*;
import java.io.*;
public class Example3{
public static void main(String args[]){
try{
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
Document document=builder.parse(new File("Example3.xml"));
Element root=document.getDocumentElement();
String rooName=root.getNodeName();
System.out.println("XML文件根结点的名称为:"+rooName);
NodeList nodelist=document.getElementsByTagName("图书");
int size=nodelist.getLength();
for(int i=0;i<size;i++){
Node node=nodelist.item(i);
String name=node.getNodeName();
String content=node.getTextContent();
System.out.println(name);
System.out.println(content);
}
}
catch(Exception e){
System.out.println(e);
}
}
}
example3
<?xml version="1.0" encoding="GB2312"?>
<书店>
<图书 类别="烹饪">
<标题 语种="中文">家常菜二十一招</标题>
<作者>刘华林</作者>
<出版日期>2007</出版日期>
<价格>12.00</价格>
</图书>
</书店>
example4.java
import org.w3c.dom.*;
import javax.xml.parsers.*;
import java.io.*;
public class Example4{
public static void main(String args[]){
try{
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
Document document=builder.parse(new File("Example4.xml"));
Element root=document.getDocumentElement();
String rooName=root.getNodeName();
System.out.println("XML文件根结点的名称为:"+rooName);
NodeList nodelist=root.getChildNodes();
int size=nodelist.getLength();
for(int i=0;i<size;i++){
Node node=nodelist.item(i);
if(node.getNodeType()==Node.ELEMENT_NODE){
Element elementNode=(Element)node;
String name=elementNode.getNodeName();
String id=elementNode.getAttribute("时间");
String content=elementNode.getTextContent();
System.out.println(name+" "+id+" "+content+"/n");
}
}
}
catch(Exception e){
System.out.println(e);
}
}
}
example4
<?xml version="1.0" encoding="GB2312"?>
<日程计划>
<日程1 时间="8:00-10:00">修改文稿</日程1>
<日程2 时间="10:00-16:00">看书</日程2>
<日程3 时间="16:00-18:00">跑步</日程3>
</日程计划>
example5.java
import org.w3c.dom.*;
import javax.xml.parsers.*;
import java.io.*;
public class Example5{
public static void main(String args[]){
try{
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
Document document=builder.parse(new File("Example5.xml"));
Element root=document.getDocumentElement();
String rooName=root.getNodeName();
System.out.println("XML文件根结点的名称为:"+rooName);
NodeList nodelist=root.getChildNodes();
TotalM tt=new TotalM();//创建一个类的对象
tt.outP(nodelist);//使用创建的对象调用节点集合。
System.out.println("普通员工月收入合计"+tt.sumP);
System.out.println("经理月收入合计"+tt.sumB);
System.out.println("所有员工月收入合计"+tt.sumA);
}
catch(Exception e){
System.out.println(e);
}
}
}
class TotalM{
double sumA,sumP,sumB;
public void outP(NodeList nodelist){
int size=nodelist.getLength();
for(int i=0;i<size;i++){
Node node=nodelist.item(i);
if(node.getNodeType()==Node.TEXT_NODE){
Text textNode=(Text)node;
String content=textNode.getWholeText();
System.out.print(content);
Element parent=(Element)textNode.getParentNode();
if(parent.getNodeName().equals("月薪")){
sumA=sumA+Double.parseDouble(content.trim());
String str=parent.getAttribute("职务");
if(str.equals("普通员工"))
sumP=sumP+Double.parseDouble(content.trim());
if(str.equals("经理"))
sumB=sumB+Double.parseDouble(content.trim());
}
}
if(node.getNodeType()==Node.ELEMENT_NODE){
Element elementNode=(Element)node;
String name=elementNode.getNodeName();
System.out.print(name);
NodeList nodes=elementNode.getChildNodes();
outP(nodes);
}
}
}
}
example5
<?xml version="1.0" encoding="Gb2312"?>
<收入调查表>
<姓名>刘海松
<月薪 职务="经理">2000</月薪>
</姓名>
<姓名>刘红霞
<月薪 职务="普通员工">879</月薪>
</姓名>
<姓名>李张利
<月薪 职务="经理">3200</月薪>
</姓名>
<姓名>陈凡灵
<月薪 职务="普通员工">1680</月薪>
</姓名>
</收入调查表>
example6.java
import org.w3c.dom.*;
import javax.xml.parsers.*;
import java.io.*;
public class Example6{
public static void main(String args[]){
try{
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
Document document=builder.parse(new File("Example6.xml"));
Element root=document.getDocumentElement();
String rooName=root.getNodeName();//获的根结点的名称
System.out.println("XML文件根结点的名称为:"+rooName);
NodeList nodelist=root.getElementsByTagName("员工");//获得标记名为学生的所有的标记集合
int size=nodelist.getLength();
for(int i=0;i<size;i++){
Node node=nodelist.item(i);
String name=node.getNodeName();
NamedNodeMap map=node.getAttributes();//获得标记中属性的集合。
String content=node.getTextContent();
System.out.print(name);
for(int k=0;k<map.getLength();k++){//循环的形式输出标记中所有的属性
Attr attrNode=(Attr)map.item(k);
String attName=attrNode.getName();
String attValue=attrNode.getValue();
System.out.print(" "+attName+"="+attValue);
}
System.out.print(content);
}
}
catch(Exception e){
System.out.println(e);
}
}
}
example6
<?xml version="1.0" encoding="GB2312" ?>
<员工名单>
<员工 姓名="赵妍" 年龄="30" 性别="女">
项目经理
</员工>
<员工 姓名="王冰" 年龄="32" 性别="男">
普通员工
</员工>
</员工名单>
example7.java
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import javax.xml.transform.dom.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import java.io.*;
public class Example7{
public static void main(String args[]){
try{
String train[]={"XML实践教程","JSP从入门到精通","Java实践教程"};
String type[]={"978-7-302-15488-4","7-302-12591-0","978-7-302-14337-6"};
String startTime[]={"王峰","刘海松","李章帅"};
//创建XML文档中需要的数据
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
Document document=builder.newDocument(); //创建document节点对象
document.setXmlVersion("1.0");//设置使用XML文件的版本
Element root=document.createElement("图书列表");
document.appendChild(root);//设置XML文件的根结点
for(int k=1;k<=train.length;k++){
root.appendChild(document.createElement("图书"));
} //在根节点下添加了三个节点
NodeList nodeList=document.getElementsByTagName("图书");//获得图书的节点集合
int size=nodeList.getLength();
for(int k=0;k<size;k++){
Node node=nodeList.item(k);
if(node.getNodeType()==Node.ELEMENT_NODE)
{
Element elementNode=(Element)node;
elementNode.setAttribute("ISBN",type[k]);//为图书设置属性其取值从数组type中取。
elementNode.appendChild(document.createElement("名称"));//为图书添加一个名字标记
elementNode.appendChild(document.createElement("作者"));//为图书添加一个开车时间标记
}
}
nodeList=document.getElementsByTagName("名称");//获得名字的节点集合
size=nodeList.getLength();
for(int k=0;k<size;k++){
Node node=nodeList.item(k);
if(node.getNodeType()==Node.ELEMENT_NODE){
Element elementNode=(Element)node;
elementNode.appendChild(document.createTextNode(train[k])); //为标记添加文本数据。
}
}
nodeList=document.getElementsByTagName("作者");
size=nodeList.getLength();
for(int k=0;k<size;k++){
Node node=nodeList.item(k);
if(node.getNodeType()==Node.ELEMENT_NODE){
Element elementNode=(Element)node;
elementNode.appendChild(document.createTextNode(startTime[k]));
}
}
TransformerFactory transFactory=TransformerFactory.newInstance();//创建一个TransformerFactory(转换工厂对象)
Transformer transformer=transFactory.newTransformer();//创建一个Transformer对像(文件转换对象)
DOMSource domSource=new DOMSource(document); //把要转换的Document对象封装到一个DOMSource类中
File file=new File("图书列表.xml");
FileOutputStream out=new FileOutputStream(file);
StreamResult xmlResult=new StreamResult(out);//将要变换得到XML文件将来保存在StreamResult
transformer.transform(domSource,xmlResult);//把节点树转换为XML文件
}
catch(Exception e){
System.out.println(e);
}
}
}
example8.java
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import javax.xml.transform.dom.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import java.io.*;
public class Example8{
public static void main(String args[]){
try{ DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
Document document=builder.parse(new File("Example8.xml"));
Element root=document.getDocumentElement();
root.appendChild(document.createElement("价格"));//为根节点添加价格标记。
NodeList nodeList=document.getElementsByTagName("价格");//获得价格节点集合
int size=nodeList.getLength();
for(int k=0;k<size;k++){
Node node=nodeList.item(k);
if(node.getNodeType()==Node.ELEMENT_NODE){
Element elementNode=(Element)node;
elementNode.appendChild(document.createTextNode("588")); //为标记添加文本数据。
}
}
nodeList=document.getElementsByTagName("出厂日期");//获得出厂日期节点集合
size=nodeList.getLength();
for(int k=0;k<size;k++){
Node node=nodeList.item(k);
if(node.getNodeType()==Node.ELEMENT_NODE){
root.removeChild(node);
}
} //删除名称为出厂日期的节点
TransformerFactory transFactory=TransformerFactory.newInstance();
Transformer transformer=transFactory.newTransformer();
DOMSource domSource=new DOMSource(document);
File file=new File("Example8.xml");
FileOutputStream out=new FileOutputStream(file);
StreamResult xmlResult=new StreamResult(out);
transformer.transform(domSource,xmlResult);
}
catch(Exception e){
System.out.println(e);
}
}
}
example8
<?xml version="1.0" encoding="utf-8" standalone="no"?><手机>
<品牌>Nokia</品牌>
<型号>5200</型号>
<价格>588</价格></手机>