Trouble parsing XML
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
I think I've got pretty close with the code so far but am having difficulty creating the first catagory object. The XML parser I've implemented adds the object once it hits the final end tag. However all children entries come before that end tag, so it blows up. I just can't think of another way of doing it?
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
I've done a bit of googling and JAXB is too large apparently to use in an android app, and apache commons digester doesn't look like it plays well. Hmmm. Found simple xml. Would I need to build a program to get the data in in the first place then serialize it? vs hand coding the xml data to my own specification?
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Bill
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
I'm going to give either xml framework like simple xml or go the DOM route? I'm giving up with my previous attempts...even if I do get it to work if I make one small change to the xml file it'll be a nightmare
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
James Gibbs wrote:I think I've got pretty close with the code so far...
James. Please DontWriteLongLines (←click); it makes your thread very hard to read.
I'd break them up myself, but there are tons of them, so I suggest you do it yourself (Use the 'Edit' button).
Thanks
Winston
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Winston Gutkowski wrote:
James Gibbs wrote:I think I've got pretty close with the code so far...
James. Please DontWriteLongLines (←click); it makes your thread very hard to read.
I'd break them up myself, but there are tons of them, so I suggest you do it yourself (Use the 'Edit' button).
Thanks
Winston
Ok done
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
That last line however doesn't work.
I'm really at the end of my tether trying to figure this out. I'm tempted just to fudge it, ignore the xml hierarchy and for each node write in the xml itself who it's parent is:
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
James Gibbs wrote:
I'm really at the end of my tether trying to figure this out. I'm tempted just to fudge it, ignore the xml hierarchy and for each node write in the xml itself who it's parent is:
Yuck. That's ugly. I would not do it that way.
If you're going to do this semi-manually using a DOM, then the processing is going to be recursive because of the fact that a BrowseNode can contain a list of children BrowseNodes and so on. The first thing is to get your object structure right.
BrowseNode has this set of attributes: id, name, List<BrowseNode> children.
Then break the problem down into component parts. The trickiest part is recursing into the next level when you get to a Children tag. It's tricky but not that tricky:
This is just off the top of my head but that's the general algorithm of the recursive processing you have to do. I've left out some things for you to figure out yourself.
Personally, I would just use the Simple XML framework for Android. It looks like it would do the job handily.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
I took another look at simple xml, and it does look great. The problem is it looks like I'd have to build the objects first, then deserialize it to xml. Which would mean I'd have to build a separate program to do a one off manual input run for each catagory. Seems like a lot of overhead when I can just write the xml out manually once?
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
James Gibbs wrote:The problem is it looks like I'd have to build the objects first, then deserialize it to xml. Which would mean I'd have to build a separate program to do a one off manual input run for each catagory. Seems like a lot of overhead when I can just write the xml out manually once?
Just to make sure we're on the same page here, deserializing means taking XML and creating an object graph/hierarchy from it, whereas serializing is taking an object graph/hierarchy and generating XML. I thought your goal was to deserialize some XML.
I'm not clear on what you mean by having to "build a separate program to do one off manual input run for each catagory[sic]" (correct spelling is category, BTW). It seems to me that after you've defined your objects and annotated them appropriately for Simple XML framework, all you would need to do is something like their deserialization example:
Am I missing something?
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
is absolutely deadly. Sometimes (frequently) that first child will be a Text type Node that you don't even think about when working on the logic.
Bill
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
William Brogden wrote:I have not really been following this but the following usage of getFirstChild():
is absolutely deadly. Sometimes (frequently) that first child will be a Text type Node that you don't even think about when working on the logic.
Bill
Thanks for the input. I didn't use that implementation in the end. By" text type node" do you mean that the text of the first child node might not be what you were expecting?
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Junilu Lacar wrote:
James Gibbs wrote:The problem is it looks like I'd have to build the objects first, then deserialize it to xml. Which would mean I'd have to build a separate program to do a one off manual input run for each catagory. Seems like a lot of overhead when I can just write the xml out manually once?
Just to make sure we're on the same page here, deserializing means taking XML and creating an object graph/hierarchy from it, whereas serializing is taking an object graph/hierarchy and generating XML. I thought your goal was to deserialize some XML.
I'm not clear on what you mean by having to "build a separate program to do one off manual input run for each catagory[sic]" (correct spelling is category, BTW). It seems to me that after you've defined your objects and annotated them appropriately for Simple XML framework, all you would need to do is something like their deserialization example:
Am I missing something?
Sorry if I'm not making much sense and thanks for trying to decode my current confused logic! My original aim was to take a xml file (which I had already hand typed) then deserialize it to my custom objects. From what I could see Simple XML didn't let you deserialize an XML file of custom objects which wasn't first serialized using Simple XML itself.
My needs have now changed and I'll calling the Amazon Product API for the XML files. I've taken another look at Simple XML and am I right in thinking I could use the templating feature to build a template which understands the schema of the xml amazon returns?
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
James Gibbs wrote:
My original aim was to take a xml file (which I had already hand typed) then deserialize it to my custom objects. From what I could see Simple XML didn't let you deserialize an XML file of custom objects which wasn't first serialized using Simple XML itself.
No, you DO NOT need XML that was first serialized by the framework in order to use the deserialization feature. XML is XML, it doesn't matter whether you created it manually or programmatically, the deserialization feature will work as long as you have the proper annotations in your objects.
My needs have now changed and I'll calling the Amazon Product API for the XML files. I've taken another look at Simple XML and am I right in thinking I could use the templating feature to build a template which understands the schema of the xml amazon returns?
I doubt that the templating feature is what you need. The templating feature is useful for when you have XML that contains placeholder tokens of the form "${token.name}" and you want to replace these tokens with actual values which you have in memory.
Coincidentally, this is something I'm actually doing right now in my current project at work. We need to send a request to a web service and we get input from the user for certain fields. Certain other fields are calculated, such as the "releaseId", so what we do is we have a RequestTemplate that we deserialize from XML. The XML that we deserialize to the RequestTemplate has placeholder tokens for the calculated fields, e.g.
We first deserialize the template XML. Then we replace any tokens with calculated values. Then we set the other attributes with values that the user entered. Finally, we serialize the RequestTemplate to get the actual XML that we need to send to the web service.
We don't use Simple XML but the idea is the same: you deserialize the template XML then you look for tokens and replace them with values that you have in memory. Simple XML makes this easy to do because all you have to do is create a filter and give it a Map of tokens and the corresponding values to substitute in their place. When you deserialize, the objects will already have appropriate fileds populated with the values substituted from the filter map.
I can't imagine that the Amazon XML has placeholder tokens in it but if it does, then you can probably use the templating feature. Otherwise, just figure out how to annotate your objects in a way that Simple XML can match it with incoming XML.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
James Gibbs wrote:
... build a template which understands the schema of the xml amazon returns?
I think you need to understand how to structure and annotate your objects for Simple XML. Post some of the Amazon XML that you're trying to deserialize and I'll try to help you define the appropriate object structure and annotations.
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Thanks for the input. I didn't use that implementation in the end. By" text type node" do you mean that the text of the first child node might not be what you were expecting?
No, I mean that the first child can be a Node of type TEXT_NODE - consider the following XML fragment:
The first child of A is NOT b but a Node of type TEXT_NODE containing three characters, cr lf space
Following Element b there is another Node of type TEXT_NODE containing two character, cr lf
Incidentally you should get familiar with the table in the JavaDocs for org.w3c.dom.Node
Bill
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Junilu Lacar wrote:
as long as you have the proper annotations in your objects.
This was completely new to me, had no idea you could use java annotations like this.
Junilu Lacar wrote:
I doubt that the templating feature is what you need. The templating feature is useful for when you have XML that contains placeholder tokens of the form "${token.name}" and you want to replace these tokens with actual values which you have in memory.
I can't imagine that the Amazon XML has placeholder tokens in it but if it does, then you can probably use the templating feature. Otherwise, just figure out how to annotate your objects in a way that Simple XML can match it with incoming XML.
Ah I see so I had completly the wrong idea of the templating/placeholder feature. Makes sense how you describe it thanks, might come in handy one day.
I've finished building a class to pull the list of BrowseNodes (list of sub-categories elements) from amazon. The XML it returns looks like this:
I'm only interested in the BrowseNodes under the Children element
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
| These are the worst of times and these are the best of times. And this is the best tiny ad: Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing |








