xml的复杂解析取值和节点插入导出合并后的xml文件

来源:互联网 发布:人工智能的评论 编辑:程序博客网 时间:2024/05/10 11:39

如果你正准备 对xml进行解析,而这个xml里面元素放置不规则,个个节点的值取得麻烦,然后又想 往这个xml中随心所欲的插入节点,导出完整的xml文件, 那么请 that

public static String matchtoAppXml(String cpxml_path){
  
   try {
    InputStream in = null;
    // 初始化XML解析
    SAXBuilder builder = new SAXBuilder();
    Document requestDoc = null;
    requestDoc = builder.build(cpxml_path);
    Element root = (Element)requestDoc.getRootElement();
    Iterator list = root.getChildren().iterator();
    while(list.hasNext()) {
     //1-取得外挂信息
     Element node = (Element) list.next();
     String s_city="";
     String s_name="";
     String s_add="";
     String s_tel="";
     //System.out.println(node.getChild("Na").getAttributeValue("v"));
     if (node.getChild("Na") != null) {
      s_name = node.getChild("Na").getAttributeValue("v");
     }
     if (node.getChild("Add") != null) {
      s_add = node.getChild("Add").getAttributeValue("v");
     }

 //查询xml重的某个字段如

- <Poi id="0">
  <Na v="上海西门子开关有限公司" />
  <Cla v="BA80" />
  <Dis v="310112" />
  <add v="上海市闵行经济技术开发区天宁路298号" />
  <Tel v="021-24030000" />
- <Other>
  <case n="from" v="elong" i="Y" />
  <case n="city" v="北京" i="Y" />
  <case n="url" v="http://www.powerproduct.com/user1/index.asp?id=12702" i="Y" />
  <case n="fax" v="021-64924606" i="Y" />
  </Other>
  </Poi>

对应代码如下
     if (XPath.selectSingleNode(node,"Other/case[@n='city']/@v")!= null) {
      Attribute s_city_att = (Attribute)(XPath.selectSingleNode(node,"Other/case[@n='city']/@v"));
      s_city = s_city_att.getValue();
     }

     
     //2-取得匹配得到的 俗名/经度纬度
     Poi poi = getPoi(s_city,s_name,s_add,s_tel);
     
     //3-插入外挂数据中
     if(poi!=null){
      Element pop=new Element("Pop");
      //pop.setText("aaaa");
      pop.setAttribute("v",poi.getName());
      node.addContent(2,pop);//第二位插入
      
      Element geo=new Element("Geo");
      geo.setAttribute("lo",Integer.toString(poi.getLongitude()));
      geo.setAttribute("la",Integer.toString(poi.getLatitude()));
      node.addContent(3,geo);
     }else{
      node.removeContent();
     }
     
    }
    //4-保存
    XMLOutputter so=new XMLOutputter();
    FileOutputStream fos = new FileOutputStream("d:/out.xml");  
    so.output(requestDoc,fos);

得到的out.xml的效果如下:

 <Poi id="2">    
<Na v="昆仑饭店" />

  <Pop v="昆仑饭店" />
  <Geo lo="11646273" la="3994418" />
  <Cla v="2020" />
  <Dis v="110105" />
  <add v="北京朝阳区新源南路2号" />
  <Tel v="010-65903388" />
- <Other>
  <case n="from" v="derby" i="Y" />
  <case n="city" v="北京" i="N" />
  <case n="passportCode" v="P07000301DD77B864" i="N" />
  <case n="url" v="http://api.51ditu.com" i="N" />
  </Other>
  </Poi>
    
   } catch (MalformedURLException e) {
    // TODO 自动生成 catch 块
    e.printStackTrace();
   } catch (UnsupportedEncodingException e) {
    // TODO 自动生成 catch 块
    e.printStackTrace();
   } catch (IOException e) {
    // TODO 自动生成 catch 块
    e.printStackTrace();
   } catch (JDOMException e) {
    // TODO 自动生成 catch 块
    e.printStackTrace();
   }
 


  return "";
 }

原创粉丝点击