java 之 xml 之解析工具jaxp

一、jaxp的api查看
*jaxp是javase一部分
*jaxp解析器在jdk的javax.xml.parsers包里面
*共四个类:分别针对dom和sax解析使用的类
*dom解析类:
DocumentBuilder:解析器类
*这个一个抽象类,不能new
*得到方式:DocumentBuilderFactory.newDocumentBuild();
*包含的方法:
可以解析xml:parser(String uri) uri:文件路径 返回Document对象
*返回的Document是一个接口,父节点是Node,若在Document找不到的方法,可以在Node找
*Document的方法:
*getElementsByTagName(String tagname):可以得到标签,返回集合NodeList
*createElement(String tagName): 创建标签
*createTextNode(String data):创建文本
*Node的方法
*appendChild(Node newChild):把文本添加到标签下面
*removeChild(Node oldChild):删除节点
*getParentNode():获取父节点
*NodeList的方法
*getLength():得到集合的长度
*item(int index)下标获取到具体的值
*案例:
for(int i=0;i<list.getLength;i++){list.item(i);
DocumentBuilderFactory:解析器工厂
*sax解析器:
SAXParser:解析器类
SAXParserFactory:解析器工厂

 

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class TestJaxp {
    public static void main(String[] args) throws Exception
    {
        //查询所有name元素的值
        /**
         * 1、创建解析器工厂
         * 2、根据解析器工厂创建工厂
         * 3、解析xml返回document
         **/
        //创建解析工厂
        DocumentBuilderFactory buildFactory=DocumentBuilderFactory.newInstance();
        //创建解析器
        DocumentBuilder builder=buildFactory.newDocumentBuilder();
        //解析xml返回document
        Document document=builder.parse("src/person.xml");
        //得到所有元素节点
        NodeList nodeList=document.getElementsByTagName("name");
        //遍历读取元素内容
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node name1=nodeList.item(i);
            String s=name1.getTextContent();//得到标签元素里面的内容
            System.out.println(s);
        }
        
    }
}



//添加
//1、创建解析器工厂
        //根据解析器工厂创建工厂
        //解析xml返回对象
        DocumentBuilderFactory buildFactory=DocumentBuilderFactory.newInstance();
        DocumentBuilder builder=buildFactory.newDocumentBuilder();
        Document document=builder.parse("src/test.xml");
        NodeList nodeList=document.getElementsByTagName("p1");
        Node p1=nodeList.item(0);
        Element sex1=document.createElement("sex");
        Text text1=document.createTextNode("");
        sex1.appendChild(text1);
        p1.appendChild(sex1);
        
        TransformerFactory transformerFactory=TransformerFactory.newInstance();
        Transformer transformer=transformerFactory.newTransformer();
        transformer.transform(new DOMSource(document),new StreamResult("src/test.xml"));

//修改
//1、创建解析器工厂
        //根据解析器工厂创建工厂
        //解析xml返回对象
        DocumentBuilderFactory buildFactory=DocumentBuilderFactory.newInstance();
        DocumentBuilder builder=buildFactory.newDocumentBuilder();
        Document document=builder.parse("src/test.xml");
        Node node=document.getElementsByTagName("sex").item(0);
        node.setTextContent("");
        
        TransformerFactory transformerFactory=TransformerFactory.newInstance();
        Transformer transformer=transformerFactory.newTransformer();
        transformer.transform(new DOMSource(document),new StreamResult("src/test.xml"));

//删除
//1、创建解析器工厂
        //根据解析器工厂创建工厂
        //解析xml返回对象
        DocumentBuilderFactory buildFactory=DocumentBuilderFactory.newInstance();
        DocumentBuilder builder=buildFactory.newDocumentBuilder();
        Document document=builder.parse("src/test.xml");
        Node node=document.getElementsByTagName("name").item(0);
        Node nodeParent=node.getParentNode();
        nodeParent.removeChild(node);
        TransformerFactory transformerFactory=TransformerFactory.newInstance();
        Transformer transformer=transformerFactory.newTransformer();
        transformer.transform(new DOMSource(document),new StreamResult("src/test.xml"));

//递归遍历
    //1、创建解析器工厂
        //根据解析器工厂创建工厂
        //解析xml返回对象
        DocumentBuilderFactory buildFactory=DocumentBuilderFactory.newInstance();
        DocumentBuilder builder=buildFactory.newDocumentBuilder();
        Document document=builder.parse("src/test.xml");
        list1(document);
    }

    private static void list1(Node node) {
        // TODO Auto-generated method stub
        if(node.getNodeType()==Node.ELEMENT_NODE)
        {
            System.out.println(node.getNodeName());
        }
        
        NodeList nodelist=node.getChildNodes();
        for (int i = 0; i < nodelist.getLength(); i++) {
            Node node1=nodelist.item(i);
            list1(node1);
        }
    }

 

posted @ 2018-03-08 23:21  zmztyas  阅读(283)  评论(0编辑  收藏  举报