[JAVA]XML读取
2005-11-20 18:52
309 查看
在java对XML进行处理时,读取XML文档,对其处理,这是我得一个实例代码。
import java.io.FileInputStream;
import javax.xml.parsers.*;
import org.w3c.dom.*;
/*
* Created on 2004-6-2
*java读取XML文档
*利用DoM来读取一个XML文档的内容,并将其打印出来
*/
public class TestXML {
public static void main(String[] args) {
Document doc;
DocumentBuilderFactory factory;
DocumentBuilder docBuilder;
Element root;
String elementName;
FileInputStream in;
String fileName;
try{
//get the xml file
fileName = System.getProperty("user.dir");
fileName = fileName+"/sample.xml";
in = new FileInputStream(fileName);
//解析XML文件,生成document对象
factory = DocumentBuilderFactory.newInstance();
factory.setValidating(false);
docBuilder = factory.newDocumentBuilder();
doc = docBuilder.parse(in);
//解析成功
System.out.println("parse successfull");
//获取XML文档的根节点
root = doc.getDocumentElement();
elementName = root.getNodeName();
//打印根节点的属性
printAttributes(root);
//打印该文档全部节点
System.out.println("打印全部节点");
printElement(root,0);
}
catch(Exception exp){
exp.printStackTrace();
}
}
//打印某个节点的全部属性
public static void printAttributes(Element elem){
NamedNodeMap attributes;
int i,max;
String name,value;
Node curNode;
attributes = elem.getAttributes();
max = attributes.getLength();
for(i=0;i<max;i++){
curNode = attributes.item(i);
name = curNode.getNodeName();
value = curNode.getNodeValue();
System.out.println("/t"+name+" = "+value);
}
}
//打印所有的节点的名称和值
//改方法采用递归方式打印文档的全部节点
public static void printElement(Element elem,int depth){
String elementName;
NodeList children;
int i,max;
Node curChild;
Element curElement;
String nodeName,nodeValue;
//elementName = elem.getNodeName();
//获取输入节点的全部子节点
children = elem.getChildNodes();
//按一定格式打印输入节点
for(int j=0;j<depth;j++){
System.out.print(" ");
}
printAttributes(elem);
//采用递归方式打印全部子节点
max = children.getLength();
for(i=0;i<max;i++){
curChild = children.item(i);
//递归退出条件
if(curChild instanceof Element){
curElement = (Element)curChild;
printElement(curElement,depth+1);
}
else{
nodeName = curChild.getNodeName();
nodeValue = curChild.getNodeValue();
for(int j=0;j<depth;j++)System.out.print(" ");
System.out.println(nodeName+" = "+nodeValue);
}
}
}
}
import java.io.FileInputStream;
import javax.xml.parsers.*;
import org.w3c.dom.*;
/*
* Created on 2004-6-2
*java读取XML文档
*利用DoM来读取一个XML文档的内容,并将其打印出来
*/
public class TestXML {
public static void main(String[] args) {
Document doc;
DocumentBuilderFactory factory;
DocumentBuilder docBuilder;
Element root;
String elementName;
FileInputStream in;
String fileName;
try{
//get the xml file
fileName = System.getProperty("user.dir");
fileName = fileName+"/sample.xml";
in = new FileInputStream(fileName);
//解析XML文件,生成document对象
factory = DocumentBuilderFactory.newInstance();
factory.setValidating(false);
docBuilder = factory.newDocumentBuilder();
doc = docBuilder.parse(in);
//解析成功
System.out.println("parse successfull");
//获取XML文档的根节点
root = doc.getDocumentElement();
elementName = root.getNodeName();
//打印根节点的属性
printAttributes(root);
//打印该文档全部节点
System.out.println("打印全部节点");
printElement(root,0);
}
catch(Exception exp){
exp.printStackTrace();
}
}
//打印某个节点的全部属性
public static void printAttributes(Element elem){
NamedNodeMap attributes;
int i,max;
String name,value;
Node curNode;
attributes = elem.getAttributes();
max = attributes.getLength();
for(i=0;i<max;i++){
curNode = attributes.item(i);
name = curNode.getNodeName();
value = curNode.getNodeValue();
System.out.println("/t"+name+" = "+value);
}
}
//打印所有的节点的名称和值
//改方法采用递归方式打印文档的全部节点
public static void printElement(Element elem,int depth){
String elementName;
NodeList children;
int i,max;
Node curChild;
Element curElement;
String nodeName,nodeValue;
//elementName = elem.getNodeName();
//获取输入节点的全部子节点
children = elem.getChildNodes();
//按一定格式打印输入节点
for(int j=0;j<depth;j++){
System.out.print(" ");
}
printAttributes(elem);
//采用递归方式打印全部子节点
max = children.getLength();
for(i=0;i<max;i++){
curChild = children.item(i);
//递归退出条件
if(curChild instanceof Element){
curElement = (Element)curChild;
printElement(curElement,depth+1);
}
else{
nodeName = curChild.getNodeName();
nodeValue = curChild.getNodeValue();
for(int j=0;j<depth;j++)System.out.print(" ");
System.out.println(nodeName+" = "+nodeValue);
}
}
}
}
相关文章推荐
- Java读取XML
- Java内置的xml读取简易方法
- java 读取xml
- java读取xml
- Java四种读取和创建XML文档的例子教程
- java读取xml信息
- java读取配置文件xml
- Java 利用dom方式读取、创建xml详解及实例代码
- java读取XML
- java配置文件之xml和properties简单读取实例
- spring 的 PropertyPlaceholderConfigurer读取的属性怎么访问 (java访问方式,不是xml中的占位符哦)及此类的应用
- JAVA读取XML并打印
- java读取配置文件( properties 与 xml )
- Java读取xml的四种方式
- Java读取xml文件
- 用XStream读取XML解析成javabean
- JAVA读取XML练习,JAVA读取XML文档,JAVA解析XML文档,JAVA与XML,XML文档解析,SAX (Simple API for XML)
- java读取xml(三)
- JAVA 读取XML文件
- 把Json转Xml并且用xpath读取xml元素(JAVA)