From: Arne Vajhøj on
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
From: RedGrittyBrick on
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
From: Mayeul on
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
From: bugbear on
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
From: Arne Vajhøj on
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