Dolli Ratajkovski

Greenhorn
+ Follow
since Oct 19, 2020
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Dolli Ratajkovski

I'm trying to convert SQL-query using subquery into Doctrine Query Builder format (I'm writing a project with Symfony for PHP). Here's the original SQL-query:


Here's my Doctrine query:


But I get the next error:

[Syntax Error] line 0, col 270: Error: Expected Doctrine\ORM\Query\Lexer::T_ALIASED_NAME, got 'SELECT'


What code changes should I do?
3 years ago

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.



I've added the attribute setting, the attribute 'id' is filled with values in result XML-file, but the rest of elements are still null


Here's what I changed


EDIT: I've found a solution. Thanks everyone for help!
3 years ago

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.



I've changed my code. But as a result I get an XML-file with null values in each tag. Here's the snippet



Here's the code (Update 2)
3 years ago

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).



I think it starts from line 86 in updated code
3 years ago

Tim Moores wrote:That code, too, does not create a single instance of Shoe.



But why? What am I doing wrong? Should I put startElement, characters and endElement inside List<Shoes> parseXMLfile?
3 years ago

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.



I've tried to fix the code, but I still get the same empty XML-file
3 years ago

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.



How can I re-write a code to create Shoe instances?
3 years ago

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.



Unfortunately, it creates an empty XML-file
3 years ago

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.



That's why I posted that topic, because I have no idea how to assign a value to the shoesList

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.  

3 years ago

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?
3 years ago
I'm learning SAX now. I'm trying to save parsed data from XML-file into NEW xml-file. But now I get an error

java.lang.NullPointerException
at org.example.shoesshop.parser.SAXParser5.main(SAXParser5.java:136)



Here's a code of SAX


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.

Also sharing the code of Java class for Shoes


Java Class for ShoesShop


Here's a snippet of XML-file:
3 years ago

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?
3 years ago

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
3 years ago

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.
3 years ago

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.
3 years ago