使用JAXP的DOM方式解析XML文档(能力工场)

本节内容涉及到的知识:JAXP的DOM方式解析XML文档 (后续还有SAX方式解析XML文档)

1,获取document对象的模板代码

2,将document写入到文件中去的模板代码

3,xml编程:增(增加一个节点到xml文档中去);删(删除一个xml的节点);改(修改xml中某一个节点的内容);查(查询xml文档中某个节点中的内容)

4,修改xml中某个节点的属性


//(1)获取一个节点里的内容

@Test

public void test01() throws ParserConfigurationException, SAXException, IOException{

    //(1)获取Document解析器工厂类

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    //(2)使用Document解析器工场,创建一个Document解析器

    DocumentBuilder builder = factory.newDocumentBuilder();

    //(3)使用Document解析器,解析xml文档,得到一个代表XML文档的document对象

    Document document = builder.parse(new File("src/a.xml"));


    NodeList ioList = document.getElementsByTagName("testappend");

    Node node = ioList.item(0);


    System.out.println(node.getTextContent());

}

//(2)增加一个节点

@Test

public void test02() throws ParserConfigurationException, SAXException, IOException, TransformerException{

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    DocumentBuilder builder = factory.newDocumentBuilder();

    Document document = builder.parse(new File("src/a.xml"));


    NodeList javaee = document.getElementsByTagName("javaee");

    Node appNode = document.createElement("testappend");


    appNode.setTextContent("这是一个添加节点的测试");

    javaee.item(0).appendChild(appNode);

    //下面代码的目的:将内存中代表xml文档的document对象写到文件中去

    //(1)获取一个转换器工厂

    TransformerFactory factory2 = TransformerFactory.newInstance();

    //(2)获取一个转换器

    Transformer transformer = factory2.newTransformer();

    //(3)使用转换器将内容写到文件中去

    transformer.transform(new DOMSource(document), new StreamResult(new File("src/a.xml")));

}

//(3)删除一个节点

@Test

public void test03() throws ParserConfigurationException, SAXException, IOException, TransformerException{

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    DocumentBuilder builder = factory.newDocumentBuilder();

    Document document = builder.parse(new File("src/a.xml"));


    Node testappend = document.getElementsByTagName("testappend").item(0);

    testappend.getParentNode().removeChild(testappend);


    TransformerFactory factory2 = TransformerFactory.newInstance();

    Transformer transformer = factory2.newTransformer();

    transformer.transform(new DOMSource(document), new StreamResult(new File("src/a.xml")));

}

//(4)操作节点的属性

@Test

public void test04() throws ParserConfigurationException, SAXException, IOException, TransformerException{

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    DocumentBuilder builder = factory.newDocumentBuilder();

    Document document = builder.parse(new File("src/a.xml"));


    Node ioNode = document.getElementsByTagName("io").item(0);

    Element io = null;

    if(ioNode.getNodeType()==Node.ELEMENT_NODE){

        io = (Element) ioNode;

    }


    io.setAttribute("name", "ioName");


    TransformerFactory factory2 = TransformerFactory.newInstance();

    Transformer transformer = factory2.newTransformer();

    transformer.transform(new DOMSource(document), new StreamResult(new File("src/a.xml")));

}

使用JAXP的DOM方式解析XML文档(能力工场)

上一篇:寒假学习 第13,14天 (linux 高级编程)


下一篇:2分钟搞清楚UML