导航

进入黑马day2-解析xml三种方法(1)jaxp解析

Posted on 2013-02-25 20:27  酷鱼影子  阅读(154)  评论(0编辑  收藏  举报

jaxp解析:

              //1.获取解析器工厂

              DocumentBuilderFactory  builderFactory =DocumentBuilderFactory.newInstance();

              //2.获取解析器

              DocumentBuilderbuilder = builderFactory.newDocumentBuilder();

              //3.解析book.xml

              Documentdom = builder.parse("book.xml");

             

              //获取第一本书的售价

              Stringprice = dom.getElementsByTagName("售价").item(0).getTextContent();

              System.out.println(price);

             

              //添加特价节点    

              //1、.创建特价节点,设置特价节点的标签体为 9.9元

              Elementprice2Ele = dom.createElement("特价");

              price2Ele.setTextContent("9.9元");

              //2、.获取父节点<书>

              NodebookNode = dom.getElementsByTagName("书").item(0);

              //3、将创建的<特价>节点挂载到父节点上

              bookNode.appendChild(price2Ele);

             

              在特价节点插入<作者>节点

              //1、参考节点<作者>

              NodeauthNode = dom.getElementsByTagName("作者").item(0);

              //2、在指定位置增加子节点

              bookNode.insertBefore(price2Ele,authNode);

      

              //修改节点标签体

              //1、.获取<特价>

              Nodeprice2Node = dom.getElementsByTagName("特价").item(0);

              //2、.修改<特价>节点的标签体

              price2Node.setTextContent("8.8元");

 

              //删除节点

              //1、获取想要删除的节点<特价>

              Nodeprice2Node = dom.getElementsByTagName("特价").item(0);

              //2、取想要删除的节点的父节点<书>

              NodebookNode = price2Node.getParentNode();

              //3、节点上删除子节点

              bookNode.removeChild(price2Node);

             

              //增加属性

              //1、取属性所在的<书>节点

              ElementbookNode = (Element)dom.getElementsByTagName("书").item(0);

              //2、属性

              //bookNode.setAttribute("出版社", "人民出版社");

             

              //获取属性

              //Stringvalue = bookNode.getAttribute("出版社");

              //System.out.println(value);

             

              //修改属性

              bookNode.setAttribute("出版社", "人民教育出版社");

              //删除属性

               bookNode.removeAttribute("出版社");

             

              //最后更新xml文件

              //7.1获取转换器工厂

              TransformerFactorytransformerFactory = TransformerFactory.newInstance();

              //7.2获取转换器

              Transformertransformer = transformerFactory.newTransformer();

              //transformer.setOutputProperty(OutputKeys.ENCODING,"gb2312");

              //7.3将内存中的dom转化为book.xml的xml格式的数据

              //FileOutputStreamoutputStream = new FileOutputStream("book.xml");

              FileWriterwriter = new FileWriter("book.xml");

              transformer.transform(newDOMSource(dom), new StreamResult(writer));

       }

      

       附:xml文件节点方法

      

//遍历所有的节点
@Test
public void list() throws Exception{
//1、获取解析器工厂
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
// 2、获取解析器
DocumentBuilder builder = builderFactory.newDocumentBuilder();
// 3、解析book.xml
Document dom = builder.parse("book.xml");

getNodes(dom);

}
public void getNodes(Node node){
if(node.getNodeType()==Node.ELEMENT_NODE){
System.out.println(node.getNodeName());
}
if(node.getNodeType()==Node.ELEMENT_NODE||node.getNodeType()==Node.DOCUMENT_NODE){
if(node.hasChildNodes()){
NodeList nodeList = node.getChildNodes();
for(int x = 0;x<nodeList.getLength();x++){
node = nodeList.item(x);
getNodes(node);
}
}
}
if(node.getNodeType()==Node.TEXT_NODE){
if(node.getTextContent().trim().length()!=0){
// if(!node.getTextContent().trim().equals("")){都行
System.out.println(node.getTextContent());}
}
}

}