XML之DOM4、JDom等解析方式

前端之家收集整理的这篇文章主要介绍了XML之DOM4、JDom等解析方式前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
1、生成XML
Java代码
  1. publicvoidcreateApplicationConfigXML(){
  2. //建立document对象
  3. try{
  4. Documentdocument=DocumentHelper.createDocument();
  5. Elementroot=document.addElement("root");//添加文档根
  6. root.addComment("这个一个注释");//加入一行注释
  7. Elementrequest=root.addElement("request");//添加root的子节点
  8. request.addAttribute("type","cat");
  9. request.addAttribute("flow","tong");
  10. request.addAttribute("time","2009");
  11. Elementpro=request.addElement("pro");
  12. pro.addAttribute("type","att");
  13. pro.addAttribute("name","附件");
  14. pro.addText("测试哈子");
  15. Elementcd=request.addElement("pro");
  16. cd.addAttribute("type","cd");
  17. cd.addAttribute("name","特殊字符过滤");
  18. cd.addCDATA("特殊字符");
  19. //输出全部原始数据,在编译器中显示
  20. OutputFormatformat=OutputFormat.createPrettyPrint();
  21. format.setEncoding("GBK");//根据需要设置编码
  22. XMLWriterwriter=newXMLWriter(System.out,format);
  23. document.normalize();
  24. writer.write(document);
  25. writer.close();
  26. //输出全部原始数据,并用它生成新的我们需要的XML文件
  27. XMLWriterwriter2=newXMLWriter(newFileWriter(newFile(
  28. "test.xml")),format);
  29. writer2.write(document);//输出文件
  30. writer2.close();
  31. }catch(UnsupportedEncodingExceptione){
  32. //TODOAuto-generatedcatchblock
  33. e.printStackTrace();
  34. }catch(IOExceptione){
  35. //TODOAuto-generatedcatchblock
  36. e.printStackTrace();
  37. }
  38. }

-----------------------------------------------------------------
下面是解析和修改XML
XML文件内容如下:
Xml代码
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <root>
  3. <requesttype="Pending"flowType="GENERAL"flowName="报销流程"
  4. docId="185647"flowId="16409"nodeName="报销人确认"wikId="58288"
  5. sendId="1210040"userId="1210040"createDate="2009-12-03"
  6. title="费用报销"flowCreaterId="1210040"nodeType="1"
  7. bosTime="2009-12-0309:36:15">
  8. <protype="att"></pro>
  9. <protype="textarea"name="OP_bxryj"title="处理意见"need="true"></pro>
  10. </request>
  11. </root>

修改
Java代码
  1. publicStringgetApplcationConfigFromXMLTest(){
  2. Stringvalue="";
  3. try{
  4. SAXReadersax=newSAXReader();
  5. DocumentxmlDoc=sax.read(newFile(this.UBSSDIC_PATH));
  6. Elementroot=xmlDoc.getRootElement();//根节点
  7. Iteratorit=root.elementIterator();
  8. while(it.hasNext()){
  9. Elementele=(Element)it.next();
  10. Attributeattribute=ele.attribute("type");
  11. if(attribute.getStringValue().equals("Pending")){
  12. attribute.setValue("sendread2");//修改属性节点的值
  13. }
  14. AttributeflowType=ele.attribute("flowType");
  15. flowType.detach();//删除某个属性
  16. ele.addAttribute("type","Pending");//添加一个属性节点
  17. }
  18. Elementnew_cdata=root.addElement("new_cdata");//添加一个元素
  19. new_cdata.addCDATA("tst&ree");
  20. Elementnew_ele=root.addElement("new_ele");//添加一个元素
  21. new_ele.addText("33434343");
  22. Elementobj=(Element)root.selectObject("//pro[@type='att']");//根据XPath查找元素
  23. obj.setText("测试dddddd");//修改元素的值即text节点
  24. //输出全部原始数据,在编译器中显示
  25. OutputFormatformat=OutputFormat.createPrettyPrint();
  26. format.setEncoding("GBK");
  27. XMLWriterwriter=newXMLWriter(System.out,format);
  28. writer.write(xmlDoc);
  29. writer.close();
  30. //输出全部原始数据,并用它生成新的我们需要的XML文件
  31. XMLWriterwriter2=newXMLWriter(newFileWriter(newFile(
  32. "test.xml")),format);
  33. writer2.write(xmlDoc);//输出文件
  34. writer2.close();
  35. }catch(DocumentExceptione){
  36. System.out.println(e.getMessage());
  37. e.printStackTrace();
  38. }catch(IOExceptione){
  39. e.printStackTrace();
  40. }
  41. returnvalue;
  42. }

解析
Java代码
  1. publicvoidparseApplicationConfigXML(){
  2. try{
  3. SAXReadersax=newSAXReader();
  4. DocumentxmlDoc=sax.read(newFile("E:\\20090316HPS\\Dom4jTest\\t.xml"));
  5. Elementroot=xmlDoc.getRootElement();//根节点
  6. Iteratorit=root.elementIterator("request");
  7. while(it.hasNext()){
  8. Elementrequest=(Element)it.next();
  9. System.out.println(request.getName());
  10. System.out.println(request.attributeValue("type"));
  11. System.out.println(request.attributeValue("flow"));
  12. System.out.println(request.attributeValue("time"));
  13. }
  14. System.out.println("-------------------------------------------");
  15. Listlist=root.selectNodes("//pro");
  16. for(inti=0;i<list.size();i++){
  17. Elementpro=(Element)list.get(i);
  18. System.out.println(pro.getName());
  19. System.out.println(pro.attributeValue("type"));
  20. System.out.println(pro.attributeValue("name"));
  21. System.out.println(pro.getText());
  22. System.out.println("+++++++++++++++++++++++++++++++++");
  23. }
  24. }catch(DocumentExceptione){
  25. //TODOAuto-generatedcatchblock
  26. e.printStackTrace();
  27. }
原文链接:https://www.f2er.com/xml/298531.html

猜你在找的XML相关文章