DOM、SAX和StAX只是解析方式,没有API。
JAXP是SUN提供的一套XML解析API。JAXP(Java API for XMLProcessing,意为XML处理的Java API)
JAXP很好的支持DOM和SAX解析。
JAXP开发包是JAVASE的一部分,它由java.xml、org.w3c.dom、org.xml.sax包及其子包组成
1、以DOM解析方式修改XML
1.1、将Document树保存到文件中
packagecom.rk.xml.e_jaxp_dom_write; importjava.io.File; importjavax.xml.parsers.DocumentBuilderFactory; importjavax.xml.parsers.DocumentBuilder; importorg.w3c.dom.Document; importjavax.xml.transform.TransformerFactory; importjavax.xml.transform.Transformer; importjavax.xml.transform.dom.DOMSource; importjavax.xml.transform.stream.StreamResult; /** *将Document树保存到文件中 *@authorRK * */ publicclassDemo01 { publicstaticvoidmain(String[]args)throwsException { DocumentBuilderFactorybuilderFactory=DocumentBuilderFactory.newInstance(); DocumentBuilderbuilder=builderFactory.newDocumentBuilder(); Documentdoc=builder.parse("./src/products.xml"); TransformerFactorytransformerFactory=TransformerFactory.newInstance(); Transformertransformer=transformerFactory.newTransformer(); transformer.transform(newDOMSource(doc),newStreamResult(newFile("D:/rk/abc.xml"))); System.out.println("执行结束!"); } }
1.2、封装JAXP的工具类
packagecom.rk.utils; importjavax.xml.parsers.DocumentBuilderFactory; importjavax.xml.parsers.DocumentBuilder; importorg.w3c.dom.Document; importorg.w3c.dom.NamedNodeMap; importorg.w3c.dom.Node; importorg.w3c.dom.NodeList; importjavax.xml.transform.TransformerFactory; importjavax.xml.transform.Transformer; importjava.io.File; importjavax.xml.transform.dom.DOMSource; importjavax.xml.transform.stream.StreamResult; publicclassJAXPUtil { publicstaticDocumentopenDocument(Filefile) { try { DocumentBuilderFactorybuilderFactory=DocumentBuilderFactory.newInstance(); DocumentBuilderbuilder=builderFactory.newDocumentBuilder(); Documentdoc=builder.parse(file); returndoc; } catch(Exceptione) { thrownewRuntimeException(e); } } publicstaticDocumentopenDocument(StringfilePath) { returnopenDocument(newFile(filePath)); } publicstaticvoidsaveDocument(Documentdocument,Filefile) { try { TransformerFactorytransformerFactory=TransformerFactory.newInstance(); Transformertransformer=transformerFactory.newTransformer(); transformer.transform(newDOMSource(document),newStreamResult(file)); } catch(Exceptione) { thrownewRuntimeException(e); } } publicstaticvoidsaveDocument(Documentdocument,StringfilePath) { saveDocument(document,newFile(filePath)); } publicstaticvoidtraverseDocument(Nodenode,StringBuildersb) { //1、当前元素开始 sb.append("<"+node.getNodeName()+""); //2、获取属性 if(node.hasAttributes()) { NamedNodeMapnodeMap=node.getAttributes(); for(inti=0;i<nodeMap.getLength();i++) { NodeattrNode=nodeMap.item(i); sb.append(attrNode.getNodeName()+"=\""+attrNode.getNodeValue()+"\""); } } sb.append(">"); //3、获取子节点 NodeListchildNodes=node.getChildNodes(); for(inti=0;i<childNodes.getLength();i++) { NodesubNode=childNodes.item(i); shorttype=subNode.getNodeType(); if(type==1)//当前节点是Element节点 { traverseDocument(subNode,sb); } elseif(type==3)//当前节点是Text节点 { sb.append(subNode.getTextContent()); } else { //其它情况,不做处理 } } //4、当前元素结束 sb.append("</"+node.getNodeName()+">"); } }
1.3、通过JAXP进行Document对象树的修改
packagecom.rk.xml.e_jaxp_dom_write; importorg.w3c.dom.Document; importorg.w3c.dom.Node; importorg.w3c.dom.Element; importorg.w3c.dom.Attr; importorg.w3c.dom.Text; importcom.rk.utils.JAXPUtil; /** *通过JAXP进行Document对象树的修改: *元素:添加、删除 *属性:添加、修改、删除 *文本:添加、修改 *@authorRK * */ publicclassDemo02 { publicstaticvoidmain(String[]args) { Documentdoc=JAXPUtil.openDocument("./src/products.xml"); NoderootNode=doc.getLastChild(); //元素操作:添加、删除 ElementnewProduct=doc.createElement("product");//创建新的Elements节点 rootNode.appendChild(newProduct);//添加 NodesubNode=doc.getElementById("p001"); rootNode.removeChild(subNode);//删除 //属性操作:添加、删除、修改 AttrnewId=doc.createAttribute("id");//创建Attribute节点 newId.setNodeValue("p003");//设置Attribute节点的值 newProduct.setAttributeNode(newId);//添加 newProduct.removeAttribute("id");//删除 newProduct.setAttribute("id","p004");//修改 //文本操作:添加、修改 ElementnameElem=doc.createElement("name"); TextnameText=doc.createTextNode("最后的贵族");//创建Text节点 nameElem.appendChild(nameText);//添加 ElementpriceElem=doc.createElement("price"); priceElem.setTextContent("49.99元");//修改 newProduct.appendChild(nameElem); newProduct.appendChild(priceElem); //遍历Document树,打印结果 StringBuildersb=newStringBuilder(); JAXPUtil.traverseDocument(rootNode,sb); System.out.println(sb.toString()); //将新生成的Document树输出到指定文件 JAXPUtil.saveDocument(doc,"D:/rk/result.xml"); } }