Java根据文档对象模型解析XML
来源:互联网 发布:node n 切换版本 编辑:程序博客网 时间:2024/06/14 04:43
一般会用到3种类型节点:
文件text.xml
1元素节点,2属性节点,3文本节点
package org.w3c.dom;public interface Node { // NodeType /** * The node is an <code>Element</code>. */ public static final short ELEMENT_NODE = 1; /** * The node is an <code>Attr</code>. */ public static final short ATTRIBUTE_NODE = 2; /** * The node is a <code>Text</code> node. */ public static final short TEXT_NODE = 3;
文件text.xml
<?xml version="1.0" encoding="utf-8"?><root name="根目录">我<item name="name" value="小张"/>和<item name="name" value="小王">漂亮的</item>你</root>
import java.io.ByteArrayInputStream;import java.io.IOException;import java.io.InputStream;import java.io.UnsupportedEncodingException;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.parsers.ParserConfigurationException;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;import org.w3c.dom.Text;import org.xml.sax.SAXException;public class Test {public static void main(String[] args) {showChilds();//showType();}//展示子节点,判断类型public static void showChilds() {//String xmlStr = "<?xml version=\"1.0\" encoding=\"utf-8\"?><root name=\"根目录\">成功</root>";//Document document = parse2Doc(xmlStr);Document document = parse2Doc(Test.class.getResourceAsStream("test.xml"));//根元素System.out.println("-----------根元素------------");Element root= document.getDocumentElement();System.out.println("类型:" + root.getNodeType());System.out.println("文本内容:" + root.getTextContent().replaceAll("\n", "").replace("\t", ""));System.out.println();//属性节点System.out.println("-----------属性节点------------");Attr attr1 = root.getAttributeNode("name");System.out.println("名:" + attr1.getNodeName());System.out.println("类型:" + attr1.getNodeType());System.out.println("值:" + attr1.getValue());System.out.println();//根节点下子节点(注:得到元素节点和文本节点)。“我” “和” “你” 三个文本节点System.out.println("-----------子节点------------");NodeList nodeList = root.getChildNodes();System.out.println("子节点个数:" + nodeList.getLength());for (int i = 0, size = nodeList.getLength(); i < size; i++) {Node node = nodeList.item(i);if(node.getNodeType() == Node.ELEMENT_NODE) {System.out.println("元素节点:" + ((Element)node).getAttributeNode("value"));} else if(node.getNodeType() == Node.TEXT_NODE) {System.out.println("文本节点:" + ((Text)node).getWholeText().trim());}}System.out.println();}//显示展示不同类型节点public static void showType() {Document document = parse2Doc(Test.class.getResourceAsStream("test.xml"));Element root= document.getDocumentElement();NodeList nodeList = root.getElementsByTagName("item");for (int i = 0, size = nodeList.getLength(); i < size; i++) {//元素节点Node node = nodeList.item(i);System.out.println("元素节点文本内容:" + node.getTextContent());//属性节点NamedNodeMap nodeMap = node.getAttributes();for(int j = 0, jsize = nodeMap.getLength(); j < jsize; j++) {Node attrNode = nodeMap.item(j);System.out.println("属性节点" + attrNode);}System.out.println();}}public static Document parse2Doc(InputStream xmlIns) {Document document = null;try {document = buildDocBuilder().parse(xmlIns);} catch (UnsupportedEncodingException e) {e.printStackTrace();} catch (SAXException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return document;}public static Document parse2Doc(String xmlStr) {Document document = null;try {document = buildDocBuilder().parse(new ByteArrayInputStream(xmlStr.getBytes("utf-8")));} catch (UnsupportedEncodingException e) {e.printStackTrace();} catch (SAXException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return document;}public static DocumentBuilder buildDocBuilder() {DocumentBuilder builder = null;try {builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();} catch (ParserConfigurationException e) {e.printStackTrace();}return builder;}}
0 0
- Java根据文档对象模型解析XML
- XML DOM文档对象模型解析(转)
- XML文档对象模型
- XML文档对象模型
- Android -使用DOM(文档对象模型)解析XML文件
- 了解XML文档对象模型
- XML 文档对象模型 (DOM)
- XML 文档对象模型 (DOM)
- Java解析XML文档
- JAVA 解析XML文档
- java解析xml文档
- Java解析XML文档
- java解析xml文档
- Java解析XML文档
- java解析xml文档
- 文档抽象模型xml,xml解析生成pdf,word文档
- XML之文档对象模型DOM(上)
- [ZT]XML文档对象模型手册
- jquery显示json数据
- c_结构体指针
- LoaderManager使用详解(五)---LoaderManager在Activity/Fragment中的使用分析
- 移动端高清、多屏适配方案
- 自定义一个label 实现数字自动增加,逢十进一
- Java根据文档对象模型解析XML
- hdu 1051 Wooden Sticks(贪心)
- Android中使用Handler机制更新UI的三种解决方案
- 2012年5月SAT香港真题解析
- SPI+DMA Tutorial (for Maple, Leaflabs)
- Unity3D Vector3.Dot 点乘 与 Vector3.Cross 叉乘
- IE下input text在字符超出文本框长度时,padding-left和padding-right失效
- GRE写作必备句型
- 2012年5月SAT香港真题解析