欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

对XML文档进行分析的三个实例

程序员文章站 2022-01-20 10:05:35
...
第一个实例:使用DOM访问XML的程序

package org.xml.filterExam;

import java.io.*;

import javax.xml.parsers.*;

import org.w3c.dom.*;
public class DomStudentInfo {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
try
{
DocumentBuilder db=factory.newDocumentBuilder();
File file=new File("NewFile.xml");
Document doc=db.parse(file);

NodeList nl=doc.getElementsByTagName("student");

int len=nl.getLength();
for(int i=0;i<len;i++)
{
Element element=(Element)nl.item(i);

Node elementName=element.getElementsByTagName("name").item(0);
Node elementAge=element.getElementsByTagName("age").item(0);

String name=elementName.getFirstChild().getNodeValue();
String age=elementAge.getFirstChild().getNodeValue();

System.out.println("姓名:");
System.out.println(name);
System.out.println("年龄:");
System.out.println(age);
System.out.println("-----");
}
}
catch(Exception e)
{
System.out.println("DomStudentInfo has a problem:"+e.toString());
}
}

}



第二个实例:功能是遍历文档树中的所有节点,然后依次输出节点的名字和节点的值。

package org.xml.filterExam;

import java.io.*;

import javax.xml.parsers.*;

import org.w3c.dom.*;

public class DOMPrinter {

public static void PrintNodeInfo(Node node)
{
System.out.println(node.getNodeName()+" : "+node.getNodeValue());
}

public static void printNode(Node node)
{
short nodeType=node.getNodeType();
switch(nodeType)
{
case Node.PROCESSING_INSTRUCTION_NODE:
System.out.println("------PI start------");
PrintNodeInfo(node);
System.out.println("------PI End--------");
break;
case Node.TEXT_NODE:
System.out.println("------Text Start------");
PrintNodeInfo(node);
System.out.println("------Text End------");
break;
case Node.ELEMENT_NODE:
System.out.println("------Element start------");
PrintNodeInfo(node);
System.out.println("------Element End------");

NamedNodeMap attrs=node.getAttributes();
int attrNum=attrs.getLength();
for(int i=0;i<attrNum;i++)
{
Node attr=attrs.item(i);
System.out.println("------Attribute start------");
PrintNodeInfo(attr);
System.out.println("------Attribute End------");
}
break;
default:
break;
}
Node child=node.getFirstChild();
while(child!=null)
{
printNode(child);
child=child.getNextSibling();
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
try
{
DocumentBuilder db=dbf.newDocumentBuilder();
Document doc=db.parse(new File("NewFile.xml"));
printNode(doc);
}
catch(Exception e)
{
System.out.println("DOMPrinter has a problem :"+e.toString());
}
}

}


第三个实例:添加一个结点,将修改后的信息保存到另一个XML文件中


package org.xml.filterExam;

import java.io.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.*;

public class DOMConvert {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
try
{
DocumentBuilder db=dbf.newDocumentBuilder();
Document doc=db.parse(new File("NewFile.xml"));

Element eleStu=doc.createElement("student");
Element eleName=doc.createElement("name");
Element eleAge=doc.createElement("age");

Attr attr=doc.createAttribute("sn");
attr.setValue("03");
Text textName=doc.createTextNode("haha");
Text textAge=doc.createTextNode("30");

eleName.appendChild(textName);
eleAge.appendChild(textAge);

eleStu.appendChild(eleName);
eleStu.appendChild(eleAge);
eleStu.setAttributeNode(attr);

Element eleRoot=doc.getDocumentElement();
eleRoot.appendChild(eleStu);

DOMSource source=new DOMSource(doc);

StreamResult result=new StreamResult(new File("NewFile2.xml"));

TransformerFactory tff=TransformerFactory.newInstance();

Transformer tf=tff.newTransformer();

tf.transform(source, result);

}
catch(Exception e)
{
System.out.println("DOMConvert has a problem "+e.toString());
}

}
}

相关标签: XML