java中使用JAXP解析xml
2013-01-05 09:16
465 查看
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/**
* 使用JAXP读取XML
*
* @author yuekun
*
*/
public class Demo3 {
public static void main(String[] args) {
try {
// 获取java.xml.parsers.DocumentBuilder类:为加载和分析XML文档提供接口,也就是XML分析程序接口
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
// 加载和分析XML文档,如果加载成功则返回一个org.w3c.dom.Document对象
Document document = builder.parse("src/1.xml");
NodeList nodeList = document.getElementsByTagName("*");
NamedNodeMap attributes = null;
//下面是开始遍历NodeList集合
for (int i = 0; i < nodeList.getLength(); i++) {
Node childNode = nodeList.item(i);
String str = null;
if (childNode instanceof Element) {
Element child = (Element) childNode;
if (child.getChildNodes().getLength() > 1) {
str = "Element :" + child.getTagName() + ";";
} else {
str = "Element :" + child.getTagName();
str += " : " + child.getFirstChild().getNodeValue()
+ " ";
}
attributes = child.getAttributes();
for (int j = 0; j < attributes.getLength(); j++) {
Node attr = attributes.item(j);
if (attr instanceof Attr) {
String attrName = attr.getNodeName();
String attrValue = attr.getNodeValue();
str += " Attribute " + attrName + " : "
+ attrValue + ";";
}
}
System.out.println(str);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/**
* 使用JAXP读取XML
*
* @author yuekun
*
*/
public class Demo3 {
public static void main(String[] args) {
try {
// 获取java.xml.parsers.DocumentBuilder类:为加载和分析XML文档提供接口,也就是XML分析程序接口
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
// 加载和分析XML文档,如果加载成功则返回一个org.w3c.dom.Document对象
Document document = builder.parse("src/1.xml");
NodeList nodeList = document.getElementsByTagName("*");
NamedNodeMap attributes = null;
//下面是开始遍历NodeList集合
for (int i = 0; i < nodeList.getLength(); i++) {
Node childNode = nodeList.item(i);
String str = null;
if (childNode instanceof Element) {
Element child = (Element) childNode;
if (child.getChildNodes().getLength() > 1) {
str = "Element :" + child.getTagName() + ";";
} else {
str = "Element :" + child.getTagName();
str += " : " + child.getFirstChild().getNodeValue()
+ " ";
}
attributes = child.getAttributes();
for (int j = 0; j < attributes.getLength(); j++) {
Node attr = attributes.item(j);
if (attr instanceof Attr) {
String attrName = attr.getNodeName();
String attrValue = attr.getNodeValue();
str += " Attribute " + attrName + " : "
+ attrValue + ";";
}
}
System.out.println(str);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
相关文章推荐
- 【XML解析】(1)Java下使用JAXP中的DOM解析方式对XML文档进行解析
- java中使用JAXP解析xml(三)对XML文档中进行增、删、改
- java中使用JAXP解析xml(一)
- java中使用JAXP解析xml(二)向XML文档中添加标签
- Java 使用 Dom4j 解析 XML 指南
- java基础-使用dom4j解析XML文件
- 使用JAXP对XML文档进行DOM解析
- java使用dom4j解析xml的笔记
- java使用dom4j和XPath解析XML与.net 操作XML小结
- java 使用 dom4j 解析 XML
- 在java中使用dom4j解析xml(示例代码)
- Java JAXP 解析XML
- 【XML解析】使用Jaxp对XML进行DOM解析
- Java使用dom4j解析XML字符串
- Ajax入门(与Java服务器交互)(三)——解析XML数据、使用级联下拉选择框
- Java——使用DOM4j解析XML文档
- 在java中使用dom4j解析xml(示例代码)
- 转载:Java使用dom4j解析XML
- 使用Digester解析XML文件和JAVA对象的映射
- [JAVA100例]070、使用JDOM解析XML