现在的位置: 首页 > 综合 > 正文

java 与 XML 交互

2013年02月27日 ⁄ 综合 ⁄ 共 2611字 ⁄ 字号 评论关闭
      前几天看了编程思想那本书,看到java与XML的交互,使用的是开源XOM,操作都很简单。上网找了一下XOM的介绍,没找到多少,就下面这么点,也许大家都用“dom4j”了吧!
      
XOM虽然也是一种面向对象的XML API,类似于DOM 的风格,但是它有一些与众不同的特性比如严格保持内存中对象的不变性,从而使XOM实例总是能序列化为正确的XML。此外,与其他Java XML API相比,XOM 追求更简单和更正规。
      下面直接看代码使用吧!!
       生成XML:
  1. package test;
  2. import java.io.BufferedOutputStream;
  3. import java.io.File;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import nu.xom.*;
  7. public class XMLTest {
  8.     public static void setXML(Element root){
  9.         Document doc = new Document(root);
  10.          try {
  11.              BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("stock.xml"));
  12.              Serializer serializer = new Serializer(out, "GB2312");
  13.              serializer.setIndent(4);
  14.              serializer.setMaxLength(64);
  15.              serializer.write(doc);
  16.          }catch (IOException ex) {
  17.              ex.printStackTrace();
  18.          }
  19.     }
  20.     
  21.     public static void main(String[] args) {
  22.         Element root = new Element("root");  
  23.          for (int i = 1; i <= 5; i++) {
  24.              Element stock = new Element("stock");
  25.              Element code = new Element("code");
  26.              Element name = new Element("name");
  27.              code.appendChild("stock"+i);
  28.              name.appendChild("黄金"+i);
  29.              stock.appendChild(code);
  30.              stock.appendChild(name);
  31.              root.appendChild(stock);
  32.          }
  33.          setXML(root);
  34.      }
  35. }

     读取XML:

  1. public static String[] getXMLChildList(String FileName) {
  2.         Builder builder = new Builder();
  3.         Document doc = null;
  4.         try {
  5.             doc = builder.build(new File(FileName));
  6.         } catch (ValidityException e) {
  7.             e.printStackTrace();
  8.         } catch (ParsingException e) {
  9.             e.printStackTrace();
  10.         } catch (IOException e) {
  11.             e.printStackTrace();
  12.         }
  13.         Element root = doc.getRootElement();
  14.         String [] stockCodeArray = null
  15.         if (root != null) {
  16.             Elements stocks = root.getChildElements();
  17.             stockCodeArray = new String[stocks.size()];
  18.             for (int i = 0; i < stocks.size(); i++) {
  19.                  Element stock = stocks.get(i);
  20.                  Elements child = stock.getChildElements();
  21.                  for(int j = 0 ; j<child.size(); j++){
  22.                         String code = child.get(0).getValue();
  23.                         stockCodeArray[i] = code;
  24.                  }
  25.             }
  26.         } 
  27.         return stockCodeArray;
  28.     }        
  29.     
  30.     public static void main(String[] args) {
  31.         String [] a = getXMLChildList("stock.xml");
  32.         for(int i = 0 ; i<a.length; i++){
  33.             System.out.println(a[i]);
  34.         }
  35.    }
  36. }
            

抱歉!评论已关闭.