[Syntax Error] line 0, col 270: Error: Expected Doctrine\ORM\Query\Lexer::T_ALIASED_NAME, got 'SELECT'
Tim Moores wrote:You only added part of what the other code did. In that one, there was also code that set the other attributes. Here, you only set the flags to true, but you don't actually set the attributes.
Tim Moores wrote:No. Look at the code in https://coderanch.com/t/735782/java/StAX-Parser-creates-empty-XML. It creates a Shoe object in line 21, and then subsequently populates the other values. Then in line 55, it adds the object to the list. This code here does none of those things.
Dave Tolls wrote:It might be best if you tried to explain to us where in that code you think you are creating a Shoes object, populating it with data from the XML parsing, and adding it to the List<Shoes>?
(By the way, it would make more sense if Shoes was called Shoe, as it represent a single type of Shoe).
Tim Moores wrote:That code, too, does not create a single instance of Shoe.
Tim Moores wrote:What do you mean by "rewrite"? The code in the other topic already creates Shoe instances and adds them to a list. You need to use those lines of code in this code as well.
Dave Tolls wrote:At the moment your parser does very little.
All it does is print out what element it is currently in, and sets a flag.
It does nothing with the data.
Your original code (from the other thread https://coderanch.com/t/735782/java/StAX-Parser-creates-empty-XML) created Shoe instances.
Tim Holloway wrote:Try this:
Now you have 2 options. Either simply append Shoes as you parse them or replace the empty ShoesList with a List that has Shoes in it, whichever works best.
I recommend always initializing Collections as empty collections rather than as null. It's arguably more demanding of resources, but it simplifies a lot of logic and is good anti-bugging practice.
Paul Clapham wrote:This isn't an XML or SAX problem. It's just basic programming. First you have
And subsequently, without ever touching that variable, you have
And therefore you have a NullPointerException. To fix that, write some code which assigns a value to theshoesList variable.
I know that's not right to do like that, but now I have no idea how can I save parsed data, I can't even bring parsed data into List<> (line 125). Maybe you can help, I will be very grateful.
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.
java.lang.NullPointerException
at org.example.shoesshop.parser.SAXParser5.main(SAXParser5.java:136)
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.
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.
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.
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.