服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

jox(关于java处理xml文档的讨论)


  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();
      }
    }
  }

扫描关注微信公众号