java获取XML的所有节点
2011-11-03 13:24
357 查看
java获取XML的所有节点
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class DomParse {
public DomParse() {
//得到DOM解析器的工厂实例
//javax.xml.parsers.DocumentBuilderFactory类的实例就是我们要的解析器工厂
DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();
try {
//通过javax.xml.parsers.DocumentBuilderFactory实例的静态方法newDocumentBuilder()得到DOM解析器
DocumentBuilder dombuilder = domfac.newDocumentBuilder();
//把要解析的XML文档转化为输入流,以便DOM解析器解析它
//InputStream is = new FileInputStream("test.xml");
//解析XML文档的输入流,得到一个Document
//由XML文档的输入流得到一个org.w3c.dom.Document对象,以后的处理都是对Document对象进行的
Document doc = dombuilder.parse("test.xml");
//得到XML文档的根节点
//在DOM中只有根节点是一个org.w3c.dom.Element对象。
Element root = doc.getDocumentElement();
System.out.println(root.toString());//.getTextContent()+"--------------");
//得到节点的子节点
//这是用一个org.w3c.dom.NodeList接口来存放它所有子节点的,还有一种轮循子节点的方法
NodeList books = root.getChildNodes();
if (books != null) {
for (int i = 0; i < books.getLength(); i++) {
Node book = books.item(i);
if (book.getNodeType() == Node.ELEMENT_NODE) {
//取得节点的属性值
//注意,节点的属性也是它的子节点。它的节点类型也是Node.ELEMENT_NODE
String attribute = book.getAttributes().getNamedItem("attribute").getNodeValue();
System.out.println("attribute: "+attribute);
//轮循子节点
for (Node node = book.getFirstChild(); node != null; node = node.getNextSibling())
{
if (node.getNodeType() == Node.ELEMENT_NODE) {
if (node.getNodeName().equals("name")) {
String name = node.getFirstChild().getTextContent();
String name1 = node.getFirstChild().getNodeValue();
System.out.println("name: "+name);
System.out.println("name1: "+name1);
}
if (node.getNodeName().equals("price")) {
String price = node.getFirstChild()
.getNodeValue();
System.out.println("price"+price);
}
}
}
}
}
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
DomParse domp=new DomParse();
}
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class DomParse {
public DomParse() {
//得到DOM解析器的工厂实例
//javax.xml.parsers.DocumentBuilderFactory类的实例就是我们要的解析器工厂
DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();
try {
//通过javax.xml.parsers.DocumentBuilderFactory实例的静态方法newDocumentBuilder()得到DOM解析器
DocumentBuilder dombuilder = domfac.newDocumentBuilder();
//把要解析的XML文档转化为输入流,以便DOM解析器解析它
//InputStream is = new FileInputStream("test.xml");
//解析XML文档的输入流,得到一个Document
//由XML文档的输入流得到一个org.w3c.dom.Document对象,以后的处理都是对Document对象进行的
Document doc = dombuilder.parse("test.xml");
//得到XML文档的根节点
//在DOM中只有根节点是一个org.w3c.dom.Element对象。
Element root = doc.getDocumentElement();
System.out.println(root.toString());//.getTextContent()+"--------------");
//得到节点的子节点
//这是用一个org.w3c.dom.NodeList接口来存放它所有子节点的,还有一种轮循子节点的方法
NodeList books = root.getChildNodes();
if (books != null) {
for (int i = 0; i < books.getLength(); i++) {
Node book = books.item(i);
if (book.getNodeType() == Node.ELEMENT_NODE) {
//取得节点的属性值
//注意,节点的属性也是它的子节点。它的节点类型也是Node.ELEMENT_NODE
String attribute = book.getAttributes().getNamedItem("attribute").getNodeValue();
System.out.println("attribute: "+attribute);
//轮循子节点
for (Node node = book.getFirstChild(); node != null; node = node.getNextSibling())
{
if (node.getNodeType() == Node.ELEMENT_NODE) {
if (node.getNodeName().equals("name")) {
String name = node.getFirstChild().getTextContent();
String name1 = node.getFirstChild().getNodeValue();
System.out.println("name: "+name);
System.out.println("name1: "+name1);
}
if (node.getNodeName().equals("price")) {
String price = node.getFirstChild()
.getNodeValue();
System.out.println("price"+price);
}
}
}
}
}
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
DomParse domp=new DomParse();
}
}
相关文章推荐
- java获取XML的所有节点 .
- java 将xml格式的文本生成xml文件,并获取xml中指定节点的内容
- Java解析xml获取根节点和子节点
- 2012-09-17 → 2012-09-23 周总结:SQL Server获取XML中所有节点的属性值
- Java如何从XML文件中获取节点信息的代码解析 Document方法
- Java获取XML节点总结之读取XML文档节点的方法
- java 获取当前路径下的所有xml文档的方法
- java 获取当前路径下的所有xml文档
- Domj4读取XML文件--实现获取XML文件中所有的标签节点
- dom4j解析xml获取所有的子节点并放入map中
- Java+通过XPath获取XML文件中符合特定条件的节点
- Java接收Cordys中webservice接口的返回数据并解析xml获取相应节点数据
- java递归获取某个父节点下面的所有子节点
- [Java] 通过XPath获取XML中某个节点的属性
- JAVA获取某段时间内的所有日…
- 用LINQ获取XML节点数据
- php获取json数据所有的节点路径
- JAVA_自动获取一年内所有星期六的日期
- 递归遍历所有xml的节点及子节点
- MSSQL 下递归CTE的应用通过父级获取所有对应的子级节点(一)