Ugly SAX

Discussion in 'Java' started by Roedy Green, Feb 9, 2010.

  1. Roedy Green

    Roedy Green Guest

    I wrote a bit of code using SAX to extract data from an XML
    configuration file whose structure I composed myself. I thought to
    myself, "This can't be right". Nobody in their right mind would invent
    something so clumsy to extract the data." I used as a model various
    bits of code I found on the net. I am hoping this information was
    obsolete.

    Is there a more streamlined way to do this?

    In this case the XML file is quite small, so perhaps a DOM approach
    might be more appropriate.

    Here is the XML file I want to extract data from:

    https://wush.net/websvn/mindprod/fi...&path=/com/mindprod/htmlreflow/htmlreflow.xml

    Here is the XSD schema for the file

    https://wush.net/websvn/mindprod/fi...&path=/com/mindprod/htmlreflow/htmlreflow.xsd

    Here is my parsing code
    https://wush.net/websvn/mindprod/fi...&path=/com/mindprod/htmlreflow/Configure.java

    What bothers me is I explained in the XSD considerable detail about
    the structure of the document, but none of this knowledge is
    automatically used in extracting data.
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com

    Every compilable program in a sense works. The problem is with your unrealistic expections on what it will do.
     
    Roedy Green, Feb 9, 2010
    #1
    1. Advertising

  2. Roedy Green wrote:
    > I wrote a bit of code using SAX to extract data from an XML
    > configuration file whose structure I composed myself. I thought to
    > myself, "This can't be right". Nobody in their right mind would invent
    > something so clumsy to extract the data." I used as a model various
    > bits of code I found on the net. I am hoping this information was
    > obsolete.
    >
    > Is there a more streamlined way to do this?
    >
    > In this case the XML file is quite small, so perhaps a DOM approach
    > might be more appropriate.
    >
    > Here is the XML file I want to extract data from:
    >
    > https://wush.net/websvn/mindprod/fi...&path=/com/mindprod/htmlreflow/htmlreflow.xml
    >
    > Here is the XSD schema for the file
    >
    > https://wush.net/websvn/mindprod/fi...&path=/com/mindprod/htmlreflow/htmlreflow.xsd
    >
    > Here is my parsing code
    > https://wush.net/websvn/mindprod/fi...&path=/com/mindprod/htmlreflow/Configure.java


    Os this the code you meant to post? There's no SAX in it.

    >
    > What bothers me is I explained in the XSD considerable detail about
    > the structure of the document, but none of this knowledge is
    > automatically used in extracting data.


    To use sceham information to parse, use JAXB to generate Java classes that
    correspond to your schema types, which will also have the logic to
    deserialize themselves from XML.
     
    Mike Schilling, Feb 9, 2010
    #2
    1. Advertising

  3. Roedy Green

    Roedy Green Guest

    On Tue, 9 Feb 2010 08:12:28 -0800, "Mike Schilling"
    <> wrote, quoted or indirectly quoted
    someone who said :

    >
    >Os this the code you meant to post? There's no SAX in it.


    right. There is
    import org.xml.sax.SAXException;
    import org.xml.sax.SAXParseException;

    Which lead me to believe I was using SAX, but actually it is a DOM
    parse.

    I would rephrase my question. It the code obsolete? Is there a better
    way to do this that takes advantage of field type information in the
    XSD schema? Is there a terser way to extract data?


    --
    Roedy Green Canadian Mind Products
    http://mindprod.com

    Every compilable program in a sense works. The problem is with your unrealistic expections on what it will do.
     
    Roedy Green, Feb 9, 2010
    #3
  4. Roedy Green wrote:
    > On Tue, 9 Feb 2010 08:12:28 -0800, "Mike Schilling"
    > <> wrote, quoted or indirectly quoted
    > someone who said :
    >
    >>
    >> Os this the code you meant to post? There's no SAX in it.

    >
    > right. There is
    > import org.xml.sax.SAXException;
    > import org.xml.sax.SAXParseException;
    >
    > Which lead me to believe I was using SAX, but actually it is a DOM
    > parse.
    >
    > I would rephrase my question. It the code obsolete? Is there a better
    > way to do this that takes advantage of field type information in the
    > XSD schema? Is there a terser way to extract data?


    As I said, use JAXB to generate a class from the schema. Many web service
    toolkits can do this too (e.g. Axis), but as far as I know, they need a WSDL
    to start from.
     
    Mike Schilling, Feb 9, 2010
    #4
  5. Roedy Green

    Daniel Pitts Guest

    On 2/9/2010 7:58 AM, Roedy Green wrote:
    > What bothers me is I explained in the XSD considerable detail about
    > the structure of the document, but none of this knowledge is
    > automatically used in extracting data.


    It should bother you. Time to break out meta-programming skills. You
    can generate either the XSD from code, code from the XSD, or code+XSD
    from some other authoritative source!

    There may already be tools that do one or the other of this for you. I
    think I saw someone mention JAXB (never used it, so don't know about it).


    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
     
    Daniel Pitts, Feb 9, 2010
    #5
  6. Roedy Green

    Roedy Green Guest

    On Tue, 9 Feb 2010 10:47:10 -0800, "Mike Schilling"
    <> wrote, quoted or indirectly quoted
    someone who said :

    >As I said, use JAXB to generate a class from the schema. Many web service
    >toolkits can do this too (e.g. Axis), but as far as I know, they need a WSDL
    >to start from.


    Thanks for the tip. It turned out to be much easier than the tutorial
    lead me to expect. If you ignore the generated code, the code is much
    simpler than DOM. It gets the types right, including enums.

    I am now using it to extract data about posters that I put on my home
    page.

    I wrote up an overview on JAXB at
    http://mindprod.com/jgloss/jaxb.html

    The tool that make this feasible was Stylus Studio which generated a
    first cut at an XSD given a sample XML file. I then polished it a bit
    and tweaked it to get JAXB to generate better Java.
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com

    Every compilable program in a sense works. The problem is with your unrealistic expections on what it will do.
     
    Roedy Green, Feb 10, 2010
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Q2hhcmxpZSBEaXNvbg==?=

    Datagrid with fewer records than page size is ugly

    =?Utf-8?B?Q2hhcmxpZSBEaXNvbg==?=, Feb 29, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    456
    Alvin Bruney [MVP]
    Feb 29, 2004
  2. cr88192
    Replies:
    3
    Views:
    585
    cr88192
    Sep 7, 2005
  3. Dan Cernat
    Replies:
    18
    Views:
    490
    lilburne
    Nov 8, 2003
  4. Steven T. Hatton

    Sutter's Pimples: Good, Bad, or Ugly?

    Steven T. Hatton, Apr 11, 2004, in forum: C++
    Replies:
    24
    Views:
    1,135
    Steven T. Hatton
    Apr 12, 2004
  5. Eric Lilja
    Replies:
    3
    Views:
    368
    Sumit Rajan
    Apr 29, 2005
Loading...

Share This Page