WEB开发网
开发学院WEB开发Jsp JOX(关于Java处理xml文档的讨论) 阅读

JOX(关于Java处理xml文档的讨论)

 2008-01-05 10:17:49 来源:WEB开发网   
核心提示:JOX是一套用户xml document.和java beans之间进行数据传递的JAVA类库,一个简单直接的JOX实例:JOX是非常的简单易用,JOX(关于Java处理xml文档的讨论),假设你现在已经有了下面的Java Bean,package com.wutka.jox.test;import com.wutka

  JOX是一套用户xml document.和java beans之间进行数据传递的JAVA类库。
  
  一个简单直接的JOX实例:
  JOX是非常的简单易用。假设你现在已经有了下面的Java Bean。
  package com.wutka.jox.test;
  import com.wutka.jox.*;
  import java.util.*;
  public class TestBean implements java.io.Serializable
  {
    PRotected int foo;
    protected String bar;
    protected java.util.Date baz;
    protected Vector thingies;
    protected TestSubbean subbean;
    public TestBean()
    {
      bar = "";
      baz = new Date();
      thingies = new Vector();
    }
  
    public int getFoo() { return foo; }
    public void setFoo(int aFoo) { foo = aFoo; }
  
    public String getBar() { return bar; }
    public void setBar(String aBar) { bar = aBar; }
  
    public java.util.Date getBaz() { return baz; }
    public void setBaz(java.util.Date aBaz) { baz = aBaz; }
  
    public TestSubbean getSub() { return subbean; }
    public void setSub(TestSubbean aSub) { subbean = aSub; }
  
    public String[] getThingies()
    {
      String[] retThingies = new String[thingies.size()];
      if (thingies.size() > 0) thingies.copyInto(retThingies);
  
      return retThingies;
    }
  
    public void setThingies(String[] newThingies)
    {
      thingies = new Vector(newThingies.length);
      for (int i=0; i < newThingies.length; i++)
      {
        thingies.addElement(newThingies[i]);
      }
    }
  
    public String getThingies(int i)
    {
      return (String) thingies.elementAt(i);
    }
  
    public void setThingies(int i, String thingy)
    {
      thingies.setElementAt(thingy, i);
    }
  
    public String toString()
    {
      StringBuffer ret = new StringBuffer(
        "foo="+foo+";bar="+bar+";baz="+baz.toString()+
        ";thingies=");
      for (int i=0; i < thingies.size(); i++)
      {
        if (i > 0) ret.append(",");
        ret.append((String) thingies.elementAt(i));
      }
  
      ret.append(";sub=");
      ret.append(subbean.toString());
  
      return ret.toString();
    }
  }
  
  并且你已创建了下面的 XML 文件:
  <?xml version="1.0"?>
  <MarkTest>
  <thingies>Moe</thingies>
  <thingies>Larry</thingies>
  <thingies>Curly</thingies>
  <thingies>Shemp</thingies>
  <thingies>Curly Joe</thingies>
  <foo>5</foo>
  <baz>6/25/00 12:46 AM</baz>
  <bar>This is the bar value</bar>
  <sub>
  <age>35</age>
  <name>Mark</name>
  </sub>
  </MarkTest>
  
  下面的程序将读出XML并把数据存储于TestBean:
  package com.wutka.jox.test;
  
  import com.wutka.jox.*;
  import java.io.*;
  
  public class TestDeser
  {
    public static void main(String[] args)
    {
      try
      {
        FileInputStream in = new FileInputStream("bean.xml");
  
        JOXBeanInputStream joxIn = new JOXBeanInputStream(in);
  
        TestBean testBean = (TestBean) joxIn.readObject(
          TestBean.class);
  
        System.out.println(testBean);
      }
      catch (Exception exc)
      {
        exc.printStackTrace();
      }
    }
  }
  
  你现在能作的就是创建FileInputStream或者FileReader去读取XML文件,并且把它打包进JOXBeanInputStream或者JOXBeanReader。接着你就可以让JOX读这个对象和该对象的类。
  写一个输出XML文件的BEAN就这么简单啦^_^:
  package com.wutka.jox.test;
  
  import com.wutka.jox.*;
  import java.io.*;
  
  public class TestSer
  {
    public static void main(String[] args)
    {
      try
      {
        TestBean b = new TestBean();
        b.setFoo(5);
        b.setBar("This is the bar value");
        b.setThingies(new String[] {
          "Moe", "Larry", "Curly", "Shemp", "Curly Joe" });
        TestSubbean sub = new TestSubbean();
        sub.setName("Mark");
        sub.setAge(35);
        b.setSub(sub);
  
        FileOutputStream fileOut = new FileOutputStream("bean.xml");
        JOXBeanOutputStream joxOut = new JOXBeanOutputStream(fileOut);
  
        joxOut.writeObject("MarkTest", b);
  
        joxOut.close();
      }
      catch (Exception exc)
      {
        exc.printStackTrace();
      }
    }
  }
  
  你必须在这个Java Bean中设置一些值,创建一个用于写XML文件的输出流,用JOXBeanOutputStream或者JOXBeanWriter规定这个输出流,并写相应的对象。以后,你就不必重复做这些工作啦^_^。只要你有了DTD,JOX就可以根据这个DTD识别根标签了。
  为了后面的例子,我假定你已经创建了下面的DTD文件了:
  
  <?xml version="1.0" encoding="ISO-8859-1"?>
  
  <!ELEMENT MarkTest (Thingies*, foo?, BAR?, baz? S-U-B?)>
  
  <!ELEMENT Thingies #PCDATA>
  <!ELEMENT foo #PCDATA>
  <!ELEMENT BAR #PCDATA>
  <!ELEMENT baz #PCDATA>
  <!ELEMENT S-U-B (age)>
  <!ELEMENT age #PCDATA>
  <!ATTLIST S-U-B name CDATA "">
  
  下面的程序可以读取DTD文件并且传递给JOX,使JOX可以规范输出啦^_^:
  package com.wutka.jox.test;
  import com.wutka.jox.*;
  import com.wutka.jox.dtd.*;
  import java.io.*;
  
  public class TestSerDTD
  {
    public static void main(String[] args)
    {
      try
      {
        TestBean b = new TestBean();
        b.setFoo(5);
        b.setBar("This is the bar value");
        b.setThingies(new String[] {
          "Moe", "Larry", "Curly", "Shemp", "Curly Joe" });
        TestSubbean sub = new TestSubbean();
        sub.setName("Mark");
        sub.setAge(35);
        b.setSub(sub);
  
        FileOutputStream fileOut = new FileOutputStream("bean.xml");
  
        FileReader reader = new FileReader("testbean.dtd");
        Parser dtdParser = new Parser();
  
        DTD dtd = dtdParser.parse(reader);
        reader.close();
  
        JOXBeanOutputStream joxOut = new JOXBeanOutputStream(dtd, fileOut);
  
        joxOut.writeObject("MarkTest", b);
  
        joxOut.close();
      }
      catch (Exception exc)
      {
        exc.printStackTrace();
      }
    }
  }

Tags:JOX 关于 Java

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接