無線&移動互聯網技術研發

          換位思考·····
          posts - 19, comments - 53, trackbacks - 0, articles - 283
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          jdom 解析 xml (csdn)

          Posted on 2009-07-26 00:37 Gavin.lee 閱讀(427) 評論(0)  編輯  收藏 所屬分類: xml doc 操作

          jdom parse xml

          from:http://topic.csdn.net/t/20041231/11/3691173.html

          <?xml version="1.0" encoding="GB2312"?> 
          <specificationSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
          xsi:schemaLocation
          ="http://www.citi.qut.edu.au/yawl YAWL_Schema.xsd" 
          xmlns
          ="http://www.citi.qut.edu.au/yawl" 
          xmlns:mm
          ="www.citi.qut.edu.au/yawl/exampleSchemas/make_trip"> 
          <specification uri="Maketrip1.ywl"> 
              
          <metaData/> 
              
          <decomposition id="pay" xsi:type="問題在這里!如何處理帶有命名空間的屬性?"> 
                  
          <inputParam name="customer"> 
                      
          <type>xs:string</type> 
                  
          </inputParam> 

                  
          <inputParam name="payment_account_number"> 
                      
          <type>xs:string</type> 
                  
          </inputParam> 

                  
          <inputParam name="carDetails"> 
                      
          <type>xs:string</type> 
                  
          </inputParam> 

                  
          <inputParam name="hotelDetails"> 
                      
          <type>xs:string</type> 
                  
          </inputParam> 

                  
          <inputParam name="flightDetails"> 
                      
          <type>xs:string</type> 
                  
          </inputParam> 
              
          </decomposition> 
          </specification> 
          </specificationSet>

          package com.Gavin.tools.xml;

          import java.io.File;
          import java.util.List;

          import org.jdom.Attribute;
          import org.jdom.Document;
          import org.jdom.Element;
          import org.jdom.Namespace;
          import org.jdom.input.SAXBuilder;

          public class JdomParse {
              
          private static StringBuffer strbuf = new StringBuffer();

              
          public static void main(String[] argv) {
                  
          try {
                      SAXBuilder sb 
          = new SAXBuilder();
                      Document doc 
          = sb.build(new File("c:/jdom.xml"));
                      Element root 
          = doc.getRootElement();
                      strbuf.append(
          "<?xml   version=\"1.0\"   encoding=\"GB2312\"?>").append(System.getProperty("line.separator"));
                      strbuf.append(
          "<").append(root.getName());
                      
                      
          if (!"".equals(root.getNamespaceURI())) {// Namespace信息
                          strbuf.append("   xmlns=\"" + root.getNamespaceURI() + "\"");
                      }

                      
                      
          // 得到附加的Namespace,比如:xmlns:xsi
                      accessAdditionalNamespaces(root);
                      
          // 得到附加的Namespace的屬性,如:xsi:schemaLocation
                      accessAttribute(root);
                      
                      strbuf.append(
          ">");
                      
          // 遍歷節點!
                      accessElement(root);
                      strbuf.append(
          "</" + root.getName() + ">");
                      
                      System.out.println(strbuf.toString());
                  }
           catch (Exception e) {
                      e.printStackTrace();
                  }

              }


              
          /**
               * accessElement 遍歷結點
               * 
               * 
          @param parent
               *            Element
               
          */

              
          public static void accessElement(Element parent) {
                  List listChild 
          = parent.getChildren();
                  
          int iChild = listChild.size();
                  
          for (int i = 0; i < iChild; i++{
                      Element e 
          = (Element) listChild.get(i);
                      strbuf.append(System.getProperty(
          "line.separator"));
                      strbuf.append(
          "     <");
                      strbuf.append(e.getName());
                      
          // 訪問屬性
                      accessAttribute(e);
                      strbuf.append(
          ">");
                      strbuf.append(e.getTextTrim());
                      
          // 遞歸訪問節點
                      accessElement(e);
                      strbuf.append(
          "</");
                      strbuf.append(e.getName());
                      strbuf.append(
          ">");
                  }

                  
          // 恰當換行成XML原有的格式
                  if (iChild > 0{
                      strbuf.append(System.getProperty(
          "line.separator"));
                  }

              }


              
          /**
               * accessAttribute 訪問結點的屬性
               * 
               * 
          @param e
               *            Element
               
          */

              
          public static void accessAttribute(Element e) {
                  List listAttributes 
          = e.getAttributes();
                  
          int iAttributes = listAttributes.size();
                  
          for (int j = 0; j < iAttributes; j++{
                      Attribute attribute 
          = (Attribute) listAttributes.get(j);
                      
          // 問題:屬性帶有命名空間xsi:的怎么處理?
                      
          // <name xsi:type="Full">YuLimin</name>
                      
          // + attribute.getNamespacePrefix() + ":"
                      strbuf.append("   " + attribute.getName() + "=\""
                              + attribute.getValue() + "\"");
                  }

              }


              
          /**
               * accessAdditionalNamespaces 訪問附加的命名空間
               * 
               * 
          @param e
               *            Element
               
          */

              
          public static void accessAdditionalNamespaces(Element e) {
                  List listAdditionalNamespaces 
          = e.getAdditionalNamespaces();
                  
          int iAttributes = listAdditionalNamespaces.size();
                  
          for (int j = 0; j < iAttributes; j++{
                      Namespace namespace 
          = (Namespace) listAdditionalNamespaces.get(j);
                      strbuf.append(
          "   xmlns:" + namespace.getPrefix() + "=\""
                              + namespace.getURI() + "\"");
                  }

              }

          }

          主站蜘蛛池模板: 五指山市| 内丘县| 固安县| 晴隆县| 绥阳县| 武清区| 荆州市| 齐齐哈尔市| 鸡西市| 金川县| 托克逊县| 苏州市| 新巴尔虎右旗| 志丹县| 海原县| 怀宁县| 万荣县| 香港| 阜康市| 志丹县| 巫溪县| 淮南市| 武山县| 台山市| 嫩江县| 旬邑县| 瓦房店市| 莫力| 石阡县| 武义县| 通城县| 灵石县| 社旗县| 巴楚县| 怀宁县| 西平县| 楚雄市| 酒泉市| 印江| 四子王旗| 岳西县|