Are there any tools which generates Java code to reading XML files?

  • Permalink
  • submit to reddit
  • Email
  • Follow


Hello,

I use SAXParserFactory to read data from XML files.

Lets see some sample XML:

<cyclogram>
    <number>1</number>
    <step>
       <number>11</number>
    </step>
</cyclogram>

<cyclogram>
    <number>1</number>
    <step>
       <number>11</number>
    </step>
</cyclogram>

Since "number" is both in "cyclogram" and "step" we
need to pursue if we are actually in "cyclogram" or in "step",
to decide if "number" is connected to "cyclogram" or to
"step".

I wonder, if there are tools which could automatically
generate Java code for purpose of reading XML files.
For example, user of such tool could define structure
of XML file with use of some GUI (e.g. tree structure
graphicaly represented). After that user could press some
button and see Java code...
I hope you understand what I mean.

Regards,
  Robbo 


0
Reply Robbo 3/23/2010 11:14:27 PM

See related articles to this posting

On 23-03-2010 19:14, Robbo wrote:
> I use SAXParserFactory to read data from XML files.
>
> Lets see some sample XML:
>
> <cyclogram>
>      <number>1</number>
>      <step>
>         <number>11</number>
>      </step>
> </cyclogram>
>
> <cyclogram>
>      <number>1</number>
>      <step>
>         <number>11</number>
>      </step>
> </cyclogram>
>
> Since "number" is both in "cyclogram" and "step" we
> need to pursue if we are actually in "cyclogram" or in "step",
> to decide if "number" is connected to "cyclogram" or to
> "step".
>
> I wonder, if there are tools which could automatically
> generate Java code for purpose of reading XML files.
> For example, user of such tool could define structure
> of XML file with use of some GUI (e.g. tree structure
> graphicaly represented). After that user could press some
> button and see Java code...
> I hope you understand what I mean.

Parsing that with SAX requires you to keep context.

If you have a schema and can generate Java classes via
XJC (JAXB), then that would be a lot easier.

Arne
0
Reply ISO 3/24/2010 12:17:53 AM

On 23/03/2010 23:14, Robbo wrote:
> Hello,
>
> I use SAXParserFactory to read data from XML files.
>
> Lets see some sample XML:
>
> <cyclogram>
>      <number>1</number>
>      <step>
>         <number>11</number>
>      </step>
> </cyclogram>
>
> <cyclogram>
>      <number>1</number>
>      <step>
>         <number>11</number>
>      </step>
> </cyclogram>
>
> Since "number" is both in "cyclogram" and "step" we
> need to pursue if we are actually in "cyclogram" or in "step",
> to decide if "number" is connected to "cyclogram" or to
> "step".

DOM and XPath seem the obvious tools for this.

http://www.ibm.com/developerworks/library/x-javaxpathapi.html

-- 
RGB
0
Reply RedGrittyBrick 3/24/2010 10:18:09 AM

I would be glad, if you could tell me, what is the reason
for existing of SAXParserFactory, since there are better
(faster in coding) solutions? Somebody uses SAXParserFactory
and if yes, for what purposes?
I use SAXParserFactory and it is quite much work to
do with bunch of  "if" instructions, boolean variables...

Robbo 


0
Reply Robbo 3/24/2010 4:12:26 PM

On Wed, 24 Mar 2010 00:14:27 +0100, "Robbo" <nie.mam@yle.com> wrote,
quoted or indirectly quoted someone who said :

>
>I wonder, if there are tools which could automatically
>generate Java code for purpose of reading XML files.

That is what JAXB does, which I find the most convenient way to read
XML files.  see http://mindprod.com/jgloss/jaxb.html

The are all kinds of ways to read XML. See
http://mindprod.com/jgloss/xml.html
and follow the links.

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

Responsible Development is the style of development I aspire to now. It can be summarized by answering the question, �How would I develop if it were my money?� I�m amazed how many theoretical arguments evaporate when faced with this question. 
~ Kent Beck (born: 1961 age: 49) , evangelist for extreme programming.
0
Reply Roedy 3/24/2010 4:52:28 PM

Robbo wrote:
> I would be glad, if you could tell me, what is the reason
> for existing of SAXParserFactory, since there are better
> (faster in coding) solutions? Somebody uses SAXParserFactory
> and if yes, for what purposes?

I use it:
- Whenever an event-driven approach to read my XML is easier (admittedly 
rare.)
- Whenever my XML files are big enough that I don't want to load them 
into a DOM object. Being event-driven, SAX does not load the whole 
document in memory.
- As a base for building some ad-hoc solutions. This overlaps a lot with 
my previous reason. I imagine most solutions are based off SAX builders.

> I use SAXParserFactory and it is quite much work to
> do with bunch of  "if" instructions, boolean variables...

Yes. I usually need to wrap one or more simplification layers around it. 
Usually the layers above the first one are specialized to the XML I'm 
reading.

Rather often when you deal with XML, there are more specialized APIs 
that will address your needs better. Notably JAXB.

However, nothing is perfect for every use cases, and the game is to find 
the right tool for the given problem. Here, it looks like you'd love 
JAXB. If it somehow seems too complicated or overkill, you'd at least 
save time with a DOM approach.

(Confession: I don't have a clue about StAX. It might be good.)

--
Mayeul
0
Reply Mayeul 3/24/2010 5:04:19 PM

Arne Vajh�j wrote:
> On 23-03-2010 19:14, Robbo wrote:
>> I use SAXParserFactory to read data from XML files.
>>
>> Lets see some sample XML:
>>
>> <cyclogram>
>>      <number>1</number>
>>      <step>
>>         <number>11</number>
>>      </step>
>> </cyclogram>
>>
>> <cyclogram>
>>      <number>1</number>
>>      <step>
>>         <number>11</number>
>>      </step>
>> </cyclogram>
>>
>> Since "number" is both in "cyclogram" and "step" we
>> need to pursue if we are actually in "cyclogram" or in "step",
>> to decide if "number" is connected to "cyclogram" or to
>> "step".
>>
>> I wonder, if there are tools which could automatically
>> generate Java code for purpose of reading XML files.
>> For example, user of such tool could define structure
>> of XML file with use of some GUI (e.g. tree structure
>> graphicaly represented). After that user could press some
>> button and see Java code...
>> I hope you understand what I mean.
> 
> Parsing that with SAX requires you to keep context.


Yes - normally a simple tag stack is sufficient (push on start,
pop on end).

   BugBear
0
Reply bugbear 3/24/2010 5:09:49 PM

On Wed, 24 Mar 2010 17:12:26 +0100, "Robbo" <nie.mam@yle.com> wrote,
quoted or indirectly quoted someone who said :

>I would be glad, if you could tell me, what is the reason
>for existing of SAXParserFactory, since there are better
>(faster in coding) solutions? 

Even if it were utterly useless compared with the alternatives, it
would still have to exist to support legacy apps.

An essay is in order to explain the advantages and disadvantages of
the various XML tools, and the circumstances under which you would
best choose each one.

If anyone wants to toss some ideas into the ring, I will compile them
for the entry at http://mindprod.com/jgloss/xml.html
-- 
Roedy Green Canadian Mind Products
http://mindprod.com

Responsible Development is the style of development I aspire to now. It can be summarized by answering the question, �How would I develop if it were my money?� I�m amazed how many theoretical arguments evaporate when faced with this question. 
~ Kent Beck (born: 1961 age: 49) , evangelist for extreme programming.
0
Reply Roedy 3/24/2010 8:45:14 PM

Robbo quoted or indirectly quoted someone who said :
>> I would be glad, if you could tell me, what is the reason
>> for existing of SAXParserFactory, since there are better
>> (faster in coding) solutions?
>

'SAXParserFactory' exists for the purpose of enabling "applications to
configure and obtain a SAX based parser to parse XML documents", as
you would know if you read its Javadocs.

Roedy Green wrote:
> Even if it were utterly useless compared with the alternatives, it
> would still have to exist to support legacy apps.
>

And of course, it's far from useless.

> An essay is in order to explain the advantages and disadvantages of
> the various XML tools, and the circumstances under which you would
> best choose each one.
>
> If anyone wants to toss some ideas into the ring, I will compile them
> for the entry athttp://mindprod.com/jgloss/xml.html
>

SAX parsing is superior to DOM parsing (by which I include all the
different DOM-based approaches) when you want to represent the
information from the XML document in some structure different from the
document structure or need really, really fast parsing.  It's much,
much faster than DOM-based parsing because it pulls in the information
directly into your object model without the intermediate DOM
structure, in a single pass through the document, using much less
memory overall.  It is good when you have a specific target object
structure for the information, and don't need or want XPath/XQuery or
equivalent means of access to the document structure.  XPath and
XQuery can really slow an application down, especially as document
structures get large (approaching 1 GB).

As for DOM-based approaches being faster to code, I don't think that's
necessarily true.  A project I was on last year had its development
slowed down horribly by its attempt to use DOM parsing.  The documents
were quite large, and the geometric increase in processing time caused
by that was forcing all kinds of epicycles to try to improve things.
They'd have had a solution much faster if they'd used SAX (or StAX)
parsing.

--
Lew
0
Reply Lew 3/24/2010 9:35:39 PM

On 24-03-2010 12:12, Robbo wrote:
> I would be glad, if you could tell me, what is the reason
> for existing of SAXParserFactory, since there are better
> (faster in coding) solutions? Somebody uses SAXParserFactory
> and if yes, for what purposes?
> I use SAXParserFactory and it is quite much work to
> do with bunch of  "if" instructions, boolean variables...

SAX is great for parsing huge XML files where you
only need some of the information.

Arne
0
Reply ISO 3/24/2010 11:16:48 PM

On 24-03-2010 13:09, bugbear wrote:
> Arne Vajh�j wrote:
>> On 23-03-2010 19:14, Robbo wrote:
>>> I use SAXParserFactory to read data from XML files.
>>>
>>> Lets see some sample XML:
>>>
>>> <cyclogram>
>>> <number>1</number>
>>> <step>
>>> <number>11</number>
>>> </step>
>>> </cyclogram>
>>>
>>> <cyclogram>
>>> <number>1</number>
>>> <step>
>>> <number>11</number>
>>> </step>
>>> </cyclogram>
>>>
>>> Since "number" is both in "cyclogram" and "step" we
>>> need to pursue if we are actually in "cyclogram" or in "step",
>>> to decide if "number" is connected to "cyclogram" or to
>>> "step".
>>>
>>> I wonder, if there are tools which could automatically
>>> generate Java code for purpose of reading XML files.
>>> For example, user of such tool could define structure
>>> of XML file with use of some GUI (e.g. tree structure
>>> graphicaly represented). After that user could press some
>>> button and see Java code...
>>> I hope you understand what I mean.
>>
>> Parsing that with SAX requires you to keep context.
>
> Yes - normally a simple tag stack is sufficient (push on start,
> pop on end).

But you still need a bunch of if statements.

And the final code can easily become a bit messy.

I would prefer alternatives if they exists and are
usable in the context.

Arne
0
Reply ISO 3/24/2010 11:17:59 PM

Arne Vajhøj wrote:
> But you still need a bunch of if statements [for SAX parsing].

I've written a handful of SAX-parser based applications, starting with my 
first paid Java gig eleven years ago.  There really weren't many 'if' 
statements in them; mostly I just instantiated an object based on the tag 
being processed, using a Map to look up the appropriate handler.  In this it 
was similar to MVC code for servlets where you look up the handler based on a 
request parameter.

> And the final code can easily become a bit messy.

That's on the programmer, not the library.

> I would prefer alternatives if they exists and are
> usable in the context.

SAX is /non pareil/ for the areas where it shines.  Back in 1999, using Java 
1.2 and then-current LAN tech (no gigabit or 100Mb/s LANs then) and the 
relatively low-memory machines of the day we could process on the order of a 
million hefty documents into or out of a database in about four hours using 
SAX.  We were limited pretty much by transfer speeds not CPU because of the 
efficiency of SAX parsing.

And there weren't a lot of 'if' statements involved, no more so than any other 
app I've worked on.

-- 
Lew
0
Reply Lew 3/25/2010 12:10:02 AM

Robbo wrote:
>> I would be glad, if you could tell me, what is the reason
>> for existing of SAXParserFactory, since there are better
>> (faster in coding) solutions? Somebody uses SAXParserFactory
>> and if yes, for what purposes?
>> I use SAXParserFactory and it is quite much work to
>> do with bunch of  "if" instructions, boolean variables...

Arne Vajhøj wrote:
> SAX is great for parsing huge XML files where you
> only need some of the information.

Or all the information but not in the document's structure, or you have memory 
constraints, or you need very high speed.

-- 
Lew
0
Reply Lew 3/25/2010 12:11:35 AM

On 24-03-2010 20:11, Lew wrote:
> Robbo wrote:
>>> I would be glad, if you could tell me, what is the reason
>>> for existing of SAXParserFactory, since there are better
>>> (faster in coding) solutions? Somebody uses SAXParserFactory
>>> and if yes, for what purposes?
>>> I use SAXParserFactory and it is quite much work to
>>> do with bunch of "if" instructions, boolean variables...
>
> Arne Vajhøj wrote:
>> SAX is great for parsing huge XML files where you
>> only need some of the information.
>
> Or all the information but not in the document's structure, or you have
> memory constraints, or you need very high speed.

SAX and StAX is for huge XML files.

If I actually needs all the data, then I would tend to
prefer StAX, but if I only need some of the data, then
SAX may be better.

For a small document even though I do not need the
XML structure, then I would use DOM and XPath to pick with.
The code is more readable.

Arne
0
Reply UTF 3/25/2010 1:29:22 AM

On 24-03-2010 20:10, Lew wrote:
> Arne Vajhøj wrote:
>> But you still need a bunch of if statements [for SAX parsing].
>
> I've written a handful of SAX-parser based applications, starting with
> my first paid Java gig eleven years ago. There really weren't many 'if'
> statements in them; mostly I just instantiated an object based on the
> tag being processed, using a Map to look up the appropriate handler. In
> this it was similar to MVC code for servlets where you look up the
> handler based on a request parameter.

But in the case we are discussing, then the same tag appears in
multiple contexts. That requires if statements.

>> And the final code can easily become a bit messy.
>
> That's on the programmer, not the library.

Using SAX to parse the type of XML documents we are talking
about has to contain if statements and other solutions does
not.

>> I would prefer alternatives if they exists and are
>> usable in the context.
>
> SAX is /non pareil/ for the areas where it shines. Back in 1999, using
> Java 1.2 and then-current LAN tech (no gigabit or 100Mb/s LANs then) and
> the relatively low-memory machines of the day we could process on the
> order of a million hefty documents into or out of a database in about
> four hours using SAX. We were limited pretty much by transfer speeds not
> CPU because of the efficiency of SAX parsing.
>
> And there weren't a lot of 'if' statements involved, no more so than any
> other app I've worked on.

But given that we have a SAX_unfriendly structure of the XML
document and no indication that it is a huge file, then SAX is
not an obvious pick.

There are other cases where SAX do make sense. Even though StAX
has overtaken quite a few of those.

Arne

0
Reply UTF 3/25/2010 1:33:10 AM

Arne Vajhøj wrote:
> On 24-03-2010 20:10, Lew wrote:
>> Arne Vajhøj wrote:
>>> But you still need a bunch of if statements [for SAX parsing].
>>
>> I've written a handful of SAX-parser based applications, starting with
>> my first paid Java gig eleven years ago. There really weren't many 'if'
>> statements in them; mostly I just instantiated an object based on the
>> tag being processed, using a Map to look up the appropriate handler. In
>> this it was similar to MVC code for servlets where you look up the
>> handler based on a request parameter.
> 
> But in the case we are discussing, then the same tag appears in
> multiple contexts. That requires if statements.

Not really.

Each tag holds a reference to its enclosing tag, so it already "knows" where 
belongs without need for 'if' statements.

>>> And the final code can easily become a bit messy.
>>
>> That's on the programmer, not the library.
> 
> Using SAX to parse the type of XML documents we are talking
> about has to contain if statements and other solutions does
> not.

You are mistaken.

>>> I would prefer alternatives if they exists and are
>>> usable in the context.
>>
>> SAX is /non pareil/ for the areas where it shines. Back in 1999, using
>> Java 1.2 and then-current LAN tech (no gigabit or 100Mb/s LANs then) and
>> the relatively low-memory machines of the day we could process on the
>> order of a million hefty documents into or out of a database in about
>> four hours using SAX. We were limited pretty much by transfer speeds not
>> CPU because of the efficiency of SAX parsing.
>>
>> And there weren't a lot of 'if' statements involved, no more so than any
>> other app I've worked on.
> 
> But given that we have a SAX_unfriendly structure of the XML
> document and no indication that it is a huge file, then SAX is
> not an obvious pick.


No one XML structure is more SAX-unfriendly than another.

> There are other cases where SAX do make sense. Even though StAX
> has overtaken quite a few of those.

-- 
Lew
0
Reply Lew 3/25/2010 3:47:58 AM

Arne Vajh�j wrote:
> On 24-03-2010 13:09, bugbear wrote:
>> Arne Vajh�j wrote:
>>> On 23-03-2010 19:14, Robbo wrote:
>>>> I use SAXParserFactory to read data from XML files.
>>>>
>>>> Lets see some sample XML:
>>>>
>>>> <cyclogram>
>>>> <number>1</number>
>>>> <step>
>>>> <number>11</number>
>>>> </step>
>>>> </cyclogram>
>>>>
>>>> <cyclogram>
>>>> <number>1</number>
>>>> <step>
>>>> <number>11</number>
>>>> </step>
>>>> </cyclogram>
>>>>
>>>> Since "number" is both in "cyclogram" and "step" we
>>>> need to pursue if we are actually in "cyclogram" or in "step",
>>>> to decide if "number" is connected to "cyclogram" or to
>>>> "step".
>>>>
>>>> I wonder, if there are tools which could automatically
>>>> generate Java code for purpose of reading XML files.
>>>> For example, user of such tool could define structure
>>>> of XML file with use of some GUI (e.g. tree structure
>>>> graphicaly represented). After that user could press some
>>>> button and see Java code...
>>>> I hope you understand what I mean.
>>>
>>> Parsing that with SAX requires you to keep context.
>>
>> Yes - normally a simple tag stack is sufficient (push on start,
>> pop on end).
> 
> But you still need a bunch of if statements.
> 
> And the final code can easily become a bit messy.
> 
> I would prefer alternatives if they exists and are
> usable in the context.

One of the neatest solutions I've seen is this:

http://www.devsphere.com/xml/saxdomix/

It uses a SAX parser, but (on a configurable trigger) it will
build DOM representations of sub-trees. This sub-tree
can then be handled with the DOM technique
that suits you.

This is MASSIVELY appropriate for the common case
where an XML file is actually a set (or list)
of repeated sub-elements, e.g. a catalogue
of books, list of customer orders, etc.

You get the convenience of DOM processing on the
(e.g.) book nodes without the normal DOM overhead
of having the whole XML file in RAM at once.

     BugBear
0
Reply bugbear 3/25/2010 9:42:08 AM

On 25-03-2010 05:42, bugbear wrote:
> Arne Vajh�j wrote:
>> On 24-03-2010 13:09, bugbear wrote:
>>> Arne Vajh�j wrote:
>>>> On 23-03-2010 19:14, Robbo wrote:
>>>>> I use SAXParserFactory to read data from XML files.
>>>>>
>>>>> Lets see some sample XML:
>>>>>
>>>>> <cyclogram>
>>>>> <number>1</number>
>>>>> <step>
>>>>> <number>11</number>
>>>>> </step>
>>>>> </cyclogram>
>>>>>
>>>>> <cyclogram>
>>>>> <number>1</number>
>>>>> <step>
>>>>> <number>11</number>
>>>>> </step>
>>>>> </cyclogram>
>>>>>
>>>>> Since "number" is both in "cyclogram" and "step" we
>>>>> need to pursue if we are actually in "cyclogram" or in "step",
>>>>> to decide if "number" is connected to "cyclogram" or to
>>>>> "step".
>>>>>
>>>>> I wonder, if there are tools which could automatically
>>>>> generate Java code for purpose of reading XML files.
>>>>> For example, user of such tool could define structure
>>>>> of XML file with use of some GUI (e.g. tree structure
>>>>> graphicaly represented). After that user could press some
>>>>> button and see Java code...
>>>>> I hope you understand what I mean.
>>>>
>>>> Parsing that with SAX requires you to keep context.
>>>
>>> Yes - normally a simple tag stack is sufficient (push on start,
>>> pop on end).
>>
>> But you still need a bunch of if statements.
>>
>> And the final code can easily become a bit messy.
>>
>> I would prefer alternatives if they exists and are
>> usable in the context.
>
> One of the neatest solutions I've seen is this:
>
> http://www.devsphere.com/xml/saxdomix/
>
> It uses a SAX parser, but (on a configurable trigger) it will
> build DOM representations of sub-trees. This sub-tree
> can then be handled with the DOM technique
> that suits you.
>
> This is MASSIVELY appropriate for the common case
> where an XML file is actually a set (or list)
> of repeated sub-elements, e.g. a catalogue
> of books, list of customer orders, etc.
>
> You get the convenience of DOM processing on the
> (e.g.) book nodes without the normal DOM overhead
> of having the whole XML file in RAM at once.

That sounds pretty cool !

Arne
0
Reply ISO 3/25/2010 11:09:17 PM

On 24-03-2010 23:47, Lew wrote:
> Arne Vajhøj wrote:
>> On 24-03-2010 20:10, Lew wrote:
>>> Arne Vajhøj wrote:
>>>> But you still need a bunch of if statements [for SAX parsing].
>>>
>>> I've written a handful of SAX-parser based applications, starting with
>>> my first paid Java gig eleven years ago. There really weren't many 'if'
>>> statements in them; mostly I just instantiated an object based on the
>>> tag being processed, using a Map to look up the appropriate handler. In
>>> this it was similar to MVC code for servlets where you look up the
>>> handler based on a request parameter.
>>
>> But in the case we are discussing, then the same tag appears in
>> multiple contexts. That requires if statements.
>
> Not really.
>
> Each tag holds a reference to its enclosing tag, so it already "knows"
> where belongs without need for 'if' statements.

It does ?

How do you in startElement get a ref to the enclosing tags (potentially
recursively) ?

>>>> And the final code can easily become a bit messy.
>>>
>>> That's on the programmer, not the library.
>>
>> Using SAX to parse the type of XML documents we are talking
>> about has to contain if statements and other solutions does
>> not.
>
> You are mistaken.

It would not be the first time.

>>>> I would prefer alternatives if they exists and are
>>>> usable in the context.
>>>
>>> SAX is /non pareil/ for the areas where it shines. Back in 1999, using
>>> Java 1.2 and then-current LAN tech (no gigabit or 100Mb/s LANs then) and
>>> the relatively low-memory machines of the day we could process on the
>>> order of a million hefty documents into or out of a database in about
>>> four hours using SAX. We were limited pretty much by transfer speeds not
>>> CPU because of the efficiency of SAX parsing.
>>>
>>> And there weren't a lot of 'if' statements involved, no more so than any
>>> other app I've worked on.
>>
>> But given that we have a SAX_unfriendly structure of the XML
>> document and no indication that it is a huge file, then SAX is
>> not an obvious pick.
>
> No one XML structure is more SAX-unfriendly than another.

Correct if you can get the enclosing element as you claim.

Arne
0
Reply UTF 3/25/2010 11:13:03 PM

Arne Vajhøj wrote:
>>>>> But you still need a bunch of if statements [for SAX parsing].

Lew wrote:
>>>> I've written a handful of SAX-parser based applications, starting with
>>>> my first paid Java gig eleven years ago. There really weren't many 'if'
>>>> statements in them; mostly I just instantiated an object based on the
>>>> tag being processed, using a Map to look up the appropriate handler. In
>>>> this it was similar to MVC code for servlets where you look up the
>>>> handler based on a request parameter.

Arne Vajhøj wrote:
>>> But in the case we are discussing, then the same tag appears in
>>> multiple contexts. That requires if statements.

Lew wrote:
>> Not really.
>>
>> Each tag holds a reference to its enclosing tag, so it already "knows"
>> where belongs without need for 'if' statements.

Arne Vajhøj wrote:
> It does ?
> 
> How do you in startElement get a ref to the enclosing tags (potentially
> recursively) ?

It's been a long time since I've done one, so I don't have code samples handy. 
  I apologize; this would be so much easier if I did.

I derive from the parser class 'DefaultHandler' a custom implementation for 
each tag (element).  One member of that implementation is 'parent', which gets 
set each time an enclosing element hands off to the handler for an enclosed 
element.  You can follow the 'parent' members in a chain right back to the 
root element if you need to.

The 'endElement()' method returns control back to the enclosing handler.

This is not so very different from bugbear's suggestion that
> normally a simple tag stack is sufficient 
> (push on start, pop on end). 

It worked beautifully every time I've used it, including back in 1999 on that 
first parsing project that used a pretty decent-sized DTD for each document 
type and had quite ambitious performance goals, which we exceeded handily.

-- 
Lew
0
Reply Lew 3/26/2010 12:12:22 AM

On Wed, 24 Mar 2010, Mayeul wrote:

> (Confession: I don't have a clue about StAX. It might be good.)

It's pretty good.

tom

-- 
life finds a way
0
Reply Tom 3/26/2010 3:06:05 AM

On Wed, 24 Mar 2010, Arne Vajh?j wrote:

> On 24-03-2010 20:10, Lew wrote:
>> Arne Vajh?j wrote:
>>> But you still need a bunch of if statements [for SAX parsing].
>> 
>> I've written a handful of SAX-parser based applications, starting with 
>> my first paid Java gig eleven years ago. There really weren't many 'if' 
>> statements in them; mostly I just instantiated an object based on the 
>> tag being processed, using a Map to look up the appropriate handler. In 
>> this it was similar to MVC code for servlets where you look up the 
>> handler based on a request parameter.
>
> But in the case we are discussing, then the same tag appears in
> multiple contexts. That requires if statements.

Nope. Maintain a tag stack as a List<String>. Use the stack as a key into 
a Map<List<String>, Handler> to retrieve a handler specific to the tag in 
its context. I've written code like this, works quite nicely.

If i had to do it again, i'd use StAX instead.

tom

-- 
life finds a way
0
Reply Tom 3/26/2010 3:08:18 AM
comp.lang.java.programmer 51705 articles. 37 followers. Post

21 Replies
124 Views

Similar Articles

[PageSpeed] 27

  • Permalink
  • submit to reddit
  • Email
  • Follow


Reply:

Similar Artilces:

Generate XML file from Java
I have a custom XML format (DTD), and I would like to generate XML files from Java. I investigated Jakarta ECS which has a nice API, but it won't pretty-print -- there are no newlines in the output. Any suggestions? Kai Kai Grossjohann wrote: > I have a custom XML format (DTD), and I would like to generate XML > files from Java. I investigated Jakarta ECS which has a nice API, but > it won't pretty-print -- there are no newlines in the output. > > Any suggestions? > > Kai Transform the XML file without newline with XSLT ( Xalan for example) to a final XML...

Read XML file from Java
Hello all, I'm trying to read the contents of an XML file from my Java program. Sample: ==================================================== <tag name="First" > <Line> One </Line> <Line> Two </Line> </tag> <tag name ="Second" > <Line> Three </Line> <Line> Four </Line> </tag> ==================================================== I want to extract info from the tags and after some parsing, display them as: From first tag: This is processed info. on lines One, Two From second tag: T...

Read XML file from Java
Hi, I'm trying to read the contents of an XMl file from my Java class. Sample: ==================================================== <tag name="First" > <Line> One </Line> <Line> Two </Line> </tag> <tag name ="Second" > <Line> Three </Line> <Line> Four </Line> </tag> ==================================================== I want to extract info from the tags and after some parsing, display them as: From first tag: This is processed info. on lines One, Two From second tag: This is pr...

read xml files with java
Hello, Where can I find examples of how to read XML files with DOM johan Johan wrote: > Where can I find examples of how to read XML files with DOM You might want to have a look at the following book http://www.cafeconleche.org/books/xmljava/ it is online. -- Martin Honnen http://JavaScript.FAQTs.com/ On Sat, 07 Feb 2004 13:07:38 +0100, Johan wrote: > Hello, > > Where can I find examples of how to read XML files with DOM > > johan You could start with http://www.google.com/search?hl=en&ie=UTF-8&oe=UTF-8&q=java+reader+XML+DOM&btnG=Goog...

Reading random generated XML files
Hello! I have a folder where random XML file names are generated (each describing road accidents on different locations): The file names are totally random like: 4218ed2b-1266-4d5a-8202-1f5b017b77a5.xml 9ce17519-68a7-49f7-a549-c88a6060e1f6.xml 55bca466-583c-416c-a1eb-72b6f5f922fe.xml I want them to display on a Google Map via the GET method. Does anyone have any idea how to make the code download random generated XML file names? Muchos thanks Peter Slovenia ...

Converting Java code to XML files
Hi, I have developed this tool which can manipulate XML files ie read them, make changes, and then write them back. Now I want to experiment with Java code. Is there a tool that convert Java code to a XML file using some defined schema and convert back XML to Java? Thanks, Nikhil In article <5e5eb831-f3b9-47c7-975f- cfabd372acca@c1g2000yqi.googlegroups.com>, n.barthwal@gmail.com says... > Hi, > > I have developed this tool which can manipulate XML files ie read > them, make changes, and then write them back. Now I want to experiment > with Java co...

How do read data from generated XML file
Hi, From an example i created two arrays of data say a = [ 1 2 3 4 5] b = [ 2 4 6 8 10] the xml file i got was as follows ( saved in outDataFile.xml __________________________________ <?xml version="1.0" encoding="utf-8"?> <Node1> <DataParent> <Data1>1.000000</Data1> <Data2>2.000000</Data2> </DataParent> <DataParent> <Data1>2.000000</Data1> <Data2>4.000000</Data2> </DataParent> <DataParent> <Data1>3.000000</Data1> &...

generate java bean from an xml file?
hi.. first don't say xsl, it has some drawbacks (like being too complex) i' was wondering if there is some infrastucture like an object model or something like it that can generate javabean code (i have a custom xml file which i need to use for the source creation). On Thu, 14 Sep 2006 15:46:28 +0100, Elhanan <emaayan@hotmail.com> wrote: > hi.. > > first don't say xsl, it has some drawbacks (like being too complex) i' > was wondering if there is some infrastucture like an object model or > something like it that can generate javabean code (i have a cust...

How to generate Java Code from XML at runtime?
Hello, Experte, I'm looking for a possibility to load the Java code dynamically into a class at the run time. The Java Code should be readed from an XML-file. There are just some "If-Then" expressions and should be added into one class. Does someone know if it is possible? best regards Lucia Hello, Dorothy, thank you for your answer. I will try to explain my requirements. In my java-application there are many classes to controll the machines. These Classes contain a lot of "IF-THEN" statements. For example, if (material=True and machine=idel) then startmachine...

How to read and parse a remote XML file with Java
Hi.Using J2SE v1.4.2, I'd like to read and parse a remote file:http://foo.com/file.xmlIs it possible with Java? I'd be extremely grateful if someone couldprovide me any webpage showing a very simple code.Thank you very much. Thomas Armstrong wrote:...>Using J2SE v1.4.2, I'd like to read and parse a remote file:>http://foo.com/file.xmlIt is possible using Java 1.1, but 1.4 makes it easier in that 1.4 introduced API's specifically for parsing XML (see the javax.xml hierarchy packages for more details).>Is it possible with Java? Yes.>...I'd be extremely grateful if...

reading xml with java using config file
hi.. i'm not sure exactly about this but here goes: i would like to convert one xml format to another, i know i know use xsl, but this is more then just plain templates, and in xsl i can't even access the document i'm creating. i was thinking of creating an xml file which will have elements specifing the old element names and new ones, so my java program would run this xml file and search for element names and convert them to new ones (we are talking about doznes of elements with doeznes of attributes inside them) i remember that ibm had something called composer which did some...

Java code to convert text files to xml
Is anyone aware of a Java program preferably w/ source code to convert files in text format to xml format? Pls. let me know if you are aware of such a program. In article <6145424a.0404161056.5cbf5188@posting.google.com>, roys5@alum.rpi.edu (mave) wrote: >:Is anyone aware of a Java program preferably w/ source code to convert >:files in text format to xml format? >: >: >:Pls. let me know if you are aware of such a program. Not likely, since the details of the text format and the desired XML document structure are up to you. You just need to write something that re...

Java class codes generated from UML tools
Hello, I was just wondering if anyone here uses UML tools to generate codes from class diagrams. If so, how practical is it to become a good practice? TIA. student4lifer@gmail.com wrote: > I was just wondering if anyone here uses UML tools to generate codes > from class diagrams. If so, how practical is it to become a good > practice? TIA. There are plenty pf UML tools that can do that. If you want a free one, then use ArgoUML. Arne student4lifer@gmail.com wrote: > Hello, > > I was just wondering if anyone here uses UML tools to generate codes > from class diagr...

Reading a binary file generated from c++ code
Hello, I have some c++ code that is generating a binary file of type doubles. When I use mathematica to read the file I get the wrong data. I have successfully been able to do this with the C++ long type, but not float or double. Here is the C++ code: ofstream outfile(filename, ios::binary); outfile.write((char*)vectorData, dimension*sizeof(double)); outfile.close(); Where vectorData is simply an array of type double and dimension is the size of the array. Here is the mathematica code: ReadListBinary["c:\\test2.txt", Double] Now if you change the types to lo...

java code generation tool-good one
In my project, I need to write a lot of class like: public class Data1{ double d1; double d2; double getD1(){ return d1; } double getD2(){ return d2; } } public class data2{ int g1; int g2; int g3; int getG1(){ return g1; } int getG2(){ return g2; } int getG3(){ return g3; } } public class data3{ short aaa1; short getAAA1(){ return aaa1; } } I need to write a huge collection of code which have very clear patterns. I am wondering if there is a tool to automatically generate the code. For examp...

Need Java Code that Reads/Writes MAT-files
I am looking for a good Java class (with source code) that can read and write data (ints, floats, arrays, etc.) from and to matlab MAT-files. Thanks, Todd ...

Java/C++ Code generation via XML and XSLT
Hi there, I want to generate C++ or Java code by script, e.g. classes with getter/setter methods. Someone told me that nowadays it is done with XML and XSLT. I should use - XML File which contains classnames and private members - XSLT to transform them to code files - XSLT processor which does the conversion I have no idea of XSLT. What components do I need ? Is there someone out there who has already done something like that ? thanks Thomas On Mon, 25 Aug 2003 13:40:13 +0200, Thomas Honold <thomas.honold@siemens.com> wrote: >Hi there, > >I want to generate C++ or Jav...

Read key store file generated from Java keytools
Hi there, I need to read key store file to get private key generated by Java's KeyTools from PHP. But seams like the keystore file is not in standard format that PHP can use. I need to do so because I created a WebSevice with Java/Tomcat. Need to create secured connection between PHP client and the Java server. Anyway has similiar experience to share? Thanks! Li ...

Java reading XML files containing long values
In C you can write something like this #define something = "d�aslkdas�" \ "djalkjddlasdj" \ "asd�lask�la" \ "�dadassdl�" to prevent long definitions in one big line, not writing a long clause like: #define something = "d�aslkdas� djalkjddlasdj asd�lask�la las�dla �laskd�las �laskd�al" So, is it possible to write that kind of long values or XML tag sentences into XML file and read them in Java application as a whole row? I mean something like this: &...

How to get Java to read in XML file and parse it against DTD?
When using Java to read in an XML file, I am having problems getting the XML file to be parsed against the specified DTD. Background: I have an existing Java application that allows users to modify values in a database. To improve performance - we're looking to enable multiple record modification by using XML files. The user downloads the required records, changes them and uploads the modified file. Issue: I want to ensure that when we read the XML file in, it is parsed against the specified DTD. The XML file includes the correct DTD. The application throws FileNotFoundException when t...

How generate one file for each class in xml using xslt and java?
Hi, I have one xml file with many class elements. For each class element I need to generate a new new java file.As it is now I get ( my output ) all classes in one file. Do I control this in the code for the transformer or in the xsl-template? Where can I find information about this? Your help is very much appreciated, //Mikael Take a look at the Saxon parser. It processes the <xsl:result-document> element which will output to multiple documents. Put something like this in your xsl: <xsl:for-each select="yourClass"> <xsl:variable name="fileName" sel...

generate an output file using c code and read it in matlab
Hello every body I want to generate a file using c which when run must show some image moving on screen on a pree specified path and then i want to read the same file in matlab and the screen of matlab must show the same image moving on the screen. with regards shahid khan shahid khan <shahidkhan3@gmail.com> wrote in message <16021750.1230559239618.JavaMail.jakarta@nitrogen.mathforum.org>... > Hello every body > I want to generate a file using c which when run must show some image moving on screen on a pree specified path and then i want to read the same f...

Seeking tool to generate skeleton C++ code from header files
I am looking for a (free) tool which takes C++ Header files as its input and generates skeletons for the corresponding source code. Just empty functions are acceptable, but a templating system would be welcome. Preferably something that runs under windows, but I could always use a Linux Live CD, as I only expect to do this once. Thanks in advance ...

Write a program that reads a Java source-code file and displays all the comments.
I am trying to do exercise 17 on page 546 of Bruce Eckel's Thinking In Java (4th edition): "Write a program that reads a Java source-code file (you provide the file name on the command line) and displays all the comments." This is at the end of a section about regular expressions. We have just learnt how to use appendReplacement(). I am having great difficulty dealing with comment-markers (// or /*) inside string literals, comments that contain quotation marks, and backslashes before quotation marks. For example: System.out.println("// This is not a comment."); System....