posts - 19, comments - 53, trackbacks - 0, articles - 283
            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          jdom 解析 xml (csdn)

          Posted on 2009-07-26 00:37 Gavin.lee 閱讀(427) 評(píng)論(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(
          ">");
                      
          // 遍歷節(jié)點(diǎn)!
                      accessElement(root);
                      strbuf.append(
          "</" + root.getName() + ">");
                      
                      System.out.println(strbuf.toString());
                  }
           catch (Exception e) {
                      e.printStackTrace();
                  }

              }


              
          /**
               * accessElement 遍歷結(jié)點(diǎn)
               * 
               * 
          @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());
                      
          // 遞歸訪問節(jié)點(diǎn)
                      accessElement(e);
                      strbuf.append(
          "</");
                      strbuf.append(e.getName());
                      strbuf.append(
          ">");
                  }

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

              }


              
          /**
               * accessAttribute 訪問結(jié)點(diǎn)的屬性
               * 
               * 
          @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() + "\"");
                  }

              }

          }

          主站蜘蛛池模板: 昭通市| 广丰县| 建湖县| 都江堰市| 洪江市| 广宗县| 临湘市| 嘉义县| 平原县| 丹寨县| 英吉沙县| 许昌市| 巴青县| 云南省| 金堂县| 沙坪坝区| 定结县| 桂林市| 什邡市| 蒙阴县| 平邑县| 新巴尔虎右旗| 休宁县| 黄骅市| 潼南县| 浙江省| 小金县| 湖北省| 彭泽县| 察雅县| 铁岭县| 喀什市| 周宁县| 咸丰县| 岚皋县| 通道| 阳曲县| 兴国县| 濮阳县| 怀远县| 安溪县|