XML parsing


#1

I’ve got a simple XML document that I’m trying to parse (it’s my Last.fm weekly artist list, and unfortunately it’s not attribute based, and I’m having a lot of trouble getting it to work. Right now all I’m trying to do is read the text from each of the Artist elements, but getText() returns an empty string. I’ve done some debugging, and I’m sure I’m calling it on the right XmlElement, since the tag name is correct.

Furthermore, isTextElement() is apparently getting stuck in some kind of infinite loop when called on the Artist element, which terminates after about a minute with a breakpoint in malloc.c. When I use it on an element that isn’t a text element, it works fine.

What’s going on here? It’s a perfectly valid XML document as far as I can tell.


#2

you may want to check getAllSubText

that will do what u’re looking for


#3

If you’re sure it’s the juce parser that’s getting into an infinite loop, you could email me the file and I’ll take a look.


#4

I just realized why it was looping, and it’s not the parser (and it’s completely ridiculous that I made a mistake like that and didn’t notice), so you can ignore that part. However, I tried getAllSubText, and I’m still getting empty strings. The file is linked at the beginning of my previous post.


#5

Sorry, didn’t notice that link. Well it’s a valid enough file, but my guess is that you’re not looking at the right node. Maybe you’re using the artist node, and not its subnodes?


#6

I’m absolutely sure I’m at the right node. From what I understand from reading through the XmlDocument class, you handle the text by creating an attribute and adding it to the node’s attribute list. As you can see, there’s nothing there.


#7

No - like kraken said, you need to use getAllSubText.

It’s a bit confusing, but an element with text inside it has one or more text sub-nodes, and it’s only those that you can call getText on. getAllSubText looks for all the text sub-nodes and finds their content.


XmlElement GetText Only From Current Element
#8

getAllSubText doesn’t work for me either though. Regardless of whether I call it at the name node or the artist node.


#9

Call it on the name node and it’ll work. I just tried it on your data and it’s fine.


#10

yeah jules is right, getAllSubText SHOULD work. i’ve never had problem with it and i use massively xml documents here (parsing xml trees with values at 10th leaf).

unrelated, but you may want also to put this, just for sake of bounds check:

XmlElement* nameNode = child->getChildByName (T("name")); if (nameNode != 0) { artist = nameNode->getAllSubText(); // bounce bounce... } child = child->getNextElement();


#11

Okay, so it looks like there were actually two problems. getAllSubText() does work, but String::formatted(T("%s"), artist) doesn’t. I suppose I probably need to pass in a char* instead of a String. Anyway, thanks for the help.


#12

Ah yes, definitely not a good idea to pass a string into a printf!


#13

I didn’t realize it was just passing it forward. For some reason I assumed it was a JUCE reimplementation. :roll: This is what I get for making untested assumptions.


#14