• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Why StAX Parser creates an empty XML file

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm learning parsers in Java and now I'm trying to write a code for XML-file creation using StAX Parser and XMLStreamWriter and StringWriter. But I forced with a problem that my parser creates an empty XML-file. I think the problem is in using for-loop, but I don't know how to fix that. Here's a full code of StAX Parser. The code is can read the data from XML file and show results in console (it works perfectly) and write the data into new file (which works not properly). Please help me to fix that.



Also sharing the code of Java class for Shoes


Java Class for ShoesShop



Here's a snippet of XML-file:


This is the xml-file which I get as a result

 
Saloon Keeper
Posts: 24499
167
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Parsers don't create files. A parser consumes a file. Or something. They read, not write.

I'm out of practice, but you say you're using STaX, yet the parsing code looks more like SAX to me.
 
Dolli Ratajkovski
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:Parsers don't create files. A parser consumes a file. Or something. They read, not write.

I'm out of practice, but you say you're using STaX, yet the parsing code looks more like SAX to me.



Does it look like SAX? Anyway, I need to know how to fix the code for writing file. I need to understand this process.
 
Tim Holloway
Saloon Keeper
Posts: 24499
167
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
SAX is a generic parser.  It invokes callbacks when the beginning and end of a tag, tag attribute value or element body are encountered. You have to figure out which tag you are processing and store enough context so that when you have everything you need you can do things with that information. All while allowing for nested tags.

STaX is a targeting parser. You can use it to handle specific tags instead of having to look at everything that comes along. So it's more high-level. STaX can use a SAX parser to handle its low-level functions.

SAX is an interface specification and there are a number of SAX parsers available, any of which can generally be plugged in to a STaX parser or DOM implementation class.

XML, incidentally is case-sensitive, so you probably shouldn't be using equalsIgnoreCase() to look for tags and attributes.

I thoink that I'd probably load a DOM and write from that myself. A DOM is an in-memory 2-dimensional structure (directed acyclic graph, to be precise) that contains all the XML data that you are working with. STaX and SAX are streaming systems. They don't require as much RAM as a DOM, (usually), but a DOM allows you more freedom to cross-reference back and forth within the totality of the XML data.
 
Dolli Ratajkovski
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:SAX is a generic parser.  It invokes callbacks when the beginning and end of a tag, tag attribute value or element body are encountered. You have to figure out which tag you are processing and store enough context so that when you have everything you need you can do things with that information. All while allowing for nested tags.

STaX is a targeting parser. You can use it to handle specific tags instead of having to look at everything that comes along. So it's more high-level. STaX can use a SAX parser to handle its low-level functions.

SAX is an interface specification and there are a number of SAX parsers available, any of which can generally be plugged in to a STaX parser or DOM implementation class.

XML, incidentally is case-sensitive, so you probably shouldn't be using equalsIgnoreCase() to look for tags and attributes.

I thoink that I'd probably load a DOM and write from that myself. A DOM is an in-memory 2-dimensional structure (directed acyclic graph, to be precise) that contains all the XML data that you are working with. STaX and SAX are streaming systems. They don't require as much RAM as a DOM, (usually), but a DOM allows you more freedom to cross-reference back and forth within the totality of the XML data.



Thank you SO MUCH for this great explanation. I personally prefer DOM too. It has a lot of advantages and easier in realization. But my teacher wants me to use SAX here, I don’t understand why, maybe just in order to learn this idk. That’s why I posted this issue here.
 
Marshal
Posts: 26909
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your lines 78 to 119 are a perfectly good example of SAX serialization code. Except that you haven't handled the namespaces properly. First of all you didn't declare the {http://www.example.org/ShoesShop} namespace in the document header, and second of all if you're using namespaces you should use the overloaded versions which take the namespace URI as well as (say) the element name.

As for why there's only the root element, that's what happens when shoesShop.getShoes() returns an empty list.
 
Dolli Ratajkovski
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:Your lines 78 to 119 are a perfectly good example of SAX serialization code. Except that you haven't handled the namespaces properly. First of all you didn't declare the {http://www.example.org/ShoesShop} namespace in the document header, and second of all if you're using namespaces you should use the overloaded versions which take the namespace URI as well as (say) the element name.

As for why there's only the root element, that's what happens when shoesShop.getShoes() returns an empty list.



I've been trying to follow your advice, but I get the same result. Here's the code
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As Paul says in that quote, if there's only a root element then that's because getShoes is returning an empty list.
So that's where you need to look.

And for that you may need to debug, either by using the debugger, or by adding a load of System.out.println() calls in there so you can see what's going on.
 
Dolli Ratajkovski
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dave Tolls wrote:As Paul says in that quote, if there's only a root element then that's because getShoes is returning an empty list.
So that's where you need to look.

And for that you may need to debug, either by using the debugger, or by adding a load of System.out.println() calls in there so you can see what's going on.



I've been trying to debug, but got nothing what could be the reason of this problem. I can't understand why I get an empty list in ShoesShop. And how to use the data from existing xml-file to create a NEW xml-file.

EDIT: I've found a solution. I use loop for


not


BUT result xml file is not in UTF-8  encoding. How to fix that?
 
Tim Holloway
Saloon Keeper
Posts: 24499
167
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dolli Ratajkovski wrote:
BUT result xml file is not in UTF-8  encoding. How to fix that?



You would probably be better off using a PrintWriter. FileWriter doesn't understand character set encoding.

You could also use OutputStreamWriter, but PrintWriter supports printLine functions in an OS-independent manner.
 
Dolli Ratajkovski
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:

Dolli Ratajkovski wrote:
BUT result xml file is not in UTF-8  encoding. How to fix that?



You would probably be better off using a PrintWriter. FileWriter doesn't understand character set encoding.

You could also use OutputStreamWriter, but PrintWriter supports printLine functions in an OS-independent manner.



Thank you SO MUCH for help. I forced with a new problem with SAX. Can you take a look?
 
reply
    Bookmark Topic Watch Topic
  • New Topic