Loading ...
Sorry, an error occurred while loading the content.

RE: [pcgen-xml] Java XML serialization

Expand Messages
  • Ysgarran
    I assume that you want to use the Xerces/Xalan libraries? I ve been using JDOM just because it is very easy with that library. Brian F. ... From: Keith Davies
    Message 1 of 4 , Mar 12, 2002
    • 0 Attachment
       
      I assume that you want to use the Xerces/Xalan libraries?
      I've been using JDOM just because it is very easy with
      that library.
       
      Brian F.
       
      -----Original Message-----
      From: Keith Davies [mailto:kjdavies@...]
      Sent: Tuesday, March 12, 2002 2:57 AM
      To: pcgen-xml
      Subject: [pcgen-xml] Java XML serialization

      Hi All,

      In preparation for moving (in a couple of months) I think I may have
      accidentally packed my copy of O'Reilly's _Java & XML_.  Dammit.

      Can someone whip up a sample program showing serialization of XML in
      Java?  An example or two that I can use to load (and save) a simple
      file -- perhaps a subset of the sample.xml I posted a couple weeks ago
      would be most excellent -- would be *really* helpful.


      TIA,
      Keith
      --
      Keith Davies
      kjdavies@...

      Evil High Priest: "Leave now, if you value your skins."
      Garrick:          "I'm all for it."


      To unsubscribe from this group, send an email to:
      pcgen-xml-unsubscribe@yahoogroups.com



      Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service.
    • Keith Davies
      ... I m not particularly picky, actually. Xerces/Xalan is cool, JDOM is cool. I just want a way to load XML and create Java objects. I can roll my own using
      Message 2 of 4 , Mar 12, 2002
      • 0 Attachment
        Ysgarran wrote on Tue Mar 12 03:58:34 2002:
        >
        > --Boundary_(ID_5dQ/ne2+uwFjTtOg5NVvHQ)
        > Content-type: text/plain; charset=us-ascii
        > Content-transfer-encoding: 7BIT
        >
        >
        > I assume that you want to use the Xerces/Xalan libraries?
        > I've been using JDOM just because it is very easy with
        > that library.

        I'm not particularly picky, actually. Xerces/Xalan is cool, JDOM
        is cool. I just want a way to load XML and create Java objects.
        I can roll my own using SAX (Xerxes), but if there's a standardized
        (or at least common) method of doing so I'd rather be idiomatic.
        If there's a common library that just 'does it' that's in common
        use and freely available, I'd like to use it. No sense doing more
        work than necessary (says someone spending lots of time on a free
        project...).

        Right now I'm doing some prototype data entry screens... but there's
        no reason to throw the work away, either, so I'd like to be able to
        store the test data. If I end up not wanting it I'll get rid of it,
        but until then I'd rather save my work as I go.


        Thanks,
        Keith
        --
        Keith Davies
        kjdavies@...

        Evil High Priest: "Leave now, if you value your skins."
        Garrick: "I'm all for it."
      • Scott Ellsworth
        ... What follows is my standard demo program for xml with jdom. It is fairly current, and gets the basic idea across. The ant build script will create a jar
        Message 3 of 4 , Mar 12, 2002
        • 0 Attachment
          On Monday, March 11, 2002, at 11:57 PM, Keith Davies wrote:

          > Can someone whip up a sample program showing serialization of XML in
          > Java?

          What follows is my standard demo program for xml with jdom. It is
          fairly current, and gets the basic idea across.

          The ant build script will create a jar with all of the libraries
          bundled. You need only add jdom.jar and crimson.jar to the lib
          directory, and all should work. jdom.jar is best built from a recent
          pull from the jdom.org web site, and I used the crimson.jar that their
          cvs server gave me.

          Lay it out as follows:

          testxml
          -build.xml
          -source
          --simple.java
          -lib
          --jdom.jar
          --crimson.jar

          simple.java:

          /**
          * We are trying to build:
          <Box color="blue">
          I am a box
          <Side1>1</Side1>
          <Side2>2</Side2>
          <Side3>3</Side3>
          <Side4>4</Side4>
          </Box>
          **/

          import java.io.*;
          import org.jdom.*;
          import org.jdom.input.SAXBuilder;
          import org.jdom.output.XMLOutputter;
          import java.util.List;
          public class simple{
          public static void main(String[] args){
          writeSomeXML();
          readSomeXML();
          }
          public static void writeSomeXML(){
          Document doc=createDocument();
          writeDocument(doc);
          }
          public static void readSomeXML(){
          Document doc=readDocument();
          interpretDocument(doc);
          }
          public static Document createDocument(){
          Element root=new Element("Box");
          root.setText("I am a box");
          root.addContent(getSide(1));
          root.addContent(getSide(2));
          root.addContent(getSide(3));
          root.addContent(getSide(4));
          root.setAttribute("color", "blue");
          Document doc=new Document(root);
          return doc;
          }
          public static void writeDocument(Document doc){
          try{
          XMLOutputter out=new XMLOutputter(" ", true);
          BufferedOutputStream outstream=null;
          outstream=new BufferedOutputStream(new
          FileOutputStream("simple.xml"),1048576);
          try{
          out.output(doc, outstream);
          }
          finally {
          outstream.close();
          }
          }
          catch(IOException e) { System.out.println("Error writing
          out.xml:"+e); }
          }
          public static Element getSide(int num){
          Element element=new Element("Side"+num);
          element.setText(Integer.toString(num));
          return element;
          }
          public static Document readDocument(){
          BufferedInputStream instream=null;
          try{
          SAXBuilder builder=new SAXBuilder(false);
          instream=new BufferedInputStream(new
          FileInputStream("simple.xml"),1048576);
          try{
          return builder.build(instream);
          }
          finally{
          if (instream!=null) instream.close();
          }
          }
          catch(IOException e) { System.out.println("Error reading
          in.xml: "+e); }
          catch(JDOMException e){ System.out.println("Error parsing xml: "+e); }
          return null;
          }
          public static void interpretDocument(Document doc){
          if (doc==null) return;
          Element root=doc.getRootElement();
          if (!root.getName().equals("Box")) System.out.println("Error
          reading Box");
          Element side1=root.getChild("Side1");
          if (side1==null) System.out.println("Error reading Side1");
          else System.out.println("Side1 text value was: "+side1.getText());
          System.out.println("Side2 text value was:
          "+root.getChildText("Side2"));
          Attribute attribute=root.getAttribute("color");
          if (attribute==null) System.out.println("Error reading color");
          System.out.println("Color is "+attribute.getValue());
          }
          public static void showAllChildren(Element root){
          List elements=root.getChildren();
          for (int i=0; i<elements.size(); i++){
          Element element=(Element)elements.get(i);
          System.out.println(element.getText());
          }
          }
          }

          The build file:

          <project default="package" basedir=".">
          <target name="init">
          <tstamp/>
          <property name="src.dir" value="./source/"/>
          <property name="lib.dir" value="./lib"/>
          <property name="jar.jdom" value="${lib.dir}/jdom.jar"/>
          <property name="jar.parser" value="${lib.dir}/crimson.jar"/>
          <property name="build.dir" value="./build"/>
          <property name="build.src" value="./build/src"/>
          <property name="build.dest" value="./build/classes"/>
          <property name="dist.root" value="./dist"/>
          <property name="build.debug" value="yes"/>
          <property name="build.deprecation" value="yes"/>
          <property name="build.optimize" value="yes"/>
          <property name="jar.name" value="simple"/>
          <property name="jar.main-class" value="simple"/>
          </target>

          <!--
          =================================================================== -->
          <!-- Prepares the build
          directory -->
          <!--
          =================================================================== -->
          <target name="prepare" depends="init">
          <mkdir dir="${build.dir}"/>
          </target>

          <!--
          =================================================================== -->
          <!-- Prepares the source
          code -->
          <!--
          =================================================================== -->
          <target name="prepare-src" depends="prepare">
          <mkdir dir="${build.src}"/>
          <mkdir dir="${build.dest}"/>
          <copy todir="${build.src}">
          <fileset dir="${src.dir}"/>
          </copy>
          </target>

          <!--
          =================================================================== -->
          <!-- Compiles the source
          directory -->
          <!--
          =================================================================== -->
          <target name="compile" depends="prepare-src">
          <javac srcdir="${build.src}"
          classpath="${jar.jdom}"
          destdir="${build.dest}"
          debug="${build.debug}"
          deprecation="${build.deprecation}"
          optimize="${build.optimize}"/>
          </target>

          <!--
          =================================================================== -->
          <!-- Creates the class
          package -->
          <!--
          =================================================================== -->
          <target name="package" depends="compile">
          <jar jarfile="${build.dir}/${jar.name}.jar"
          basedir="${build.dest}">
          <manifest>
          <attribute name="Main-Class" value="${jar.main-class}"/>
          </manifest>
          <zipfileset src="${jar.jdom}"/>
          <zipfileset src="${jar.parser}"/>
          </jar>
          </target>

          <!--
          =================================================================== -->
          <!-- Clean
          targets -->
          <!--
          =================================================================== -->
          <target name="clean" depends="init">
          <delete dir="${build.dir}"/>
          </target>

          </project>

          <!-- End of file -->

          The results:


          [localhost:~/Desktop/testxml] work% ant
          [localhost:~/Desktop/testxml] work% ls build
          classes simple.jar src

          [localhost:~/Desktop/testxml] work% java -jar build/simple.jar
          Side1 text value was: 1
          Side2 text value was: 2
          Color is blue

          [localhost:~/Desktop/testxml] work% more simple.xml
          <?xml version="1.0" encoding="UTF-8"?>
          <Box color="blue">
          I am a box
          <Side1>1</Side1>
          <Side2>2</Side2>
          <Side3>3</Side3>
          <Side4>4</Side4>
          </Box>
        Your message has been successfully submitted and would be delivered to recipients shortly.