Java根据文档对象模型解析XML

来源:互联网 发布:node n 切换版本 编辑:程序博客网 时间:2024/06/14 04:43
一般会用到3种类型节点:

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>


文件Text.java

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
原创粉丝点击