qileilove

          blog已經(jīng)轉(zhuǎn)移至github,大家請?jiān)L問 http://qaseven.github.io/

          Java利用JDom解析和傳遞XML格式數(shù)據(jù)

          ava利用JDom來解析處理XML數(shù)據(jù)格式:

            需要的包jdom-1.1.2.jar

            1、將數(shù)據(jù)轉(zhuǎn)換成XML格式的數(shù)據(jù)進(jìn)行傳遞

          Element rootList, firstList, secondItem, thirdItem;
          //根元素標(biāo)簽名
          rootList = new Element("root");
          //根元素標(biāo)簽內(nèi)的屬性名與值
          rootList.setAttribute("project", pname);
          //生成Doc文檔
          Document Doc = new Document(rootList);
          //獲取文檔中的根標(biāo)簽
          rootList = Doc.getRootElement();
           
          for (int i = 0; i < judges.size(); i++)
          {   
          //生成新的元素
          firstList = new Element("flayout");
          firstList.setAttribute("percent", "percent");
          //加入根級元素中
          rootList.addContent(firstList);
          }
          XMLOutputter XMLOut = new XMLOutputter();
          //將doc文檔轉(zhuǎn)換為字符串型的XML格式
          String xmlinfo = XMLOut.outputString(Doc);
          //將開頭的去掉
          xmlinfo = xmlinfo.replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>",
          "");
          //返回已經(jīng)封裝好的XML數(shù)據(jù)
          return xmlinfo;

            2、將字符串中的XML解析出進(jìn)行處理

          //創(chuàng)建一個(gè)新的字符串
          StringReader read = new StringReader(stadXML);
          // 創(chuàng)建新的輸入源SAX 解析器將使用 InputSource 對象來確定如何讀取 XML 輸入
          InputSource source = new InputSource(read);
          // 創(chuàng)建一個(gè)新的SAXBuilder
          SAXBuilder sb = new SAXBuilder();
          String projectName;
          List<Judgestandard> standIndex = new ArrayList<Judgestandard>();
           
          try {
              // 通過輸入源構(gòu)造一個(gè)Document
              Document doc = sb.build(source);
              // 取的根元素
              Element root = doc.getRootElement();
              projectName = root.getAttributeValue("project");
              // 得到根元素所有子元素的集合
              Element et = null;
              List nodes = root.getChildren();
              // 第一級指標(biāo)
              for (int i = 0; i < nodes.size(); i++) {
                 et = (Element) nodes.get(i);// 循環(huán)依次得到子元素
                 Judgestandard judge = new Judgestandard();
          //獲取該元素中屬性的值
                 String fid = et.getAttributeValue("mainid");
                  //獲取元素的孩子數(shù)目
                 List fsize = et.getChildren();
                 // 第二級指標(biāo)
                 for (int j = 0; j < fsize.size(); j++)
          {
                     et = (Element) fsize.get(j);// 循環(huán)依次得到子元素
                     et.getAttributeValue("stdid")
                        
                 }

            Java處理XML文檔

            不需要包

            待處理的XML文檔:

          <?xml version="1.0" encoding="ISO-8859-1"?>
          <root>
          <ip>localhost</ip>
          <port>8080</port>
          </root>
          static DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
          static DocumentBuilder builder = null;
           
          builder = factory .newDocumentBuilder();
          //獲取服務(wù)器根目錄地址
          Document document = builder.parse(new File("src/ip.xml"));
          Element rootElement = document.getDocumentElement();
          NodeList list1 = rootElement.getElementsByTagName("ip");
          NodeList list2 = rootElement.getElementsByTagName("port");
          Element ip = (Element) list1.item(0); 
          Element port = (Element) list2.item(0);
          String s =ip.getFirstChild().getNodeValue().toString()+":"+port.getFirstChild().getNodeValue().toString();
          System.out.println(s);

          posted on 2013-03-01 09:54 順其自然EVO 閱讀(273) 評論(0)  編輯  收藏 所屬分類: web 前端性能測試

          <2013年3月>
          242526272812
          3456789
          10111213141516
          17181920212223
          24252627282930
          31123456

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 太仓市| 中山市| 南城县| 桐梓县| 三亚市| 宁国市| 临沂市| 神池县| 红河县| 子长县| 武清区| 安庆市| 固始县| 晋中市| 綦江县| 托克逊县| 安吉县| 怀集县| 林周县| 江永县| 涟源市| 宜城市| 浠水县| 东宁县| 宁津县| 乐业县| 麻栗坡县| 电白县| 古田县| 和平区| 稻城县| 浦东新区| 女性| 濮阳县| 襄垣县| 桃江县| 宣化县| 黑河市| 铜山县| 北宁市| 泰顺县|