iPhone : parse Lengthy XML file on the Base of Key Field - iphone

i want to parse the xml File. xml File structure is following
<?xml version="1.0" encoding="utf-8"?>
<Level>
<p id='327'>
<Item>
<Id>5877</Id>
<Type>0</Type>
<Icon>---</Icon>
<Title>Btn1Item1</Title>
</Item>
<Item>
<Id>5925</Id>
<Type>0</Type>
<Icon>---</Icon>
<Title>Btn1Item4</Title>
</Item>
</p>
<p id='328'>
<Item>
<Id>5878</Id>
<Type>0</Type>
<Icon>---</Icon>
<Title>Btn2Item1</Title>
</Item>
<Item>
<Id>5926</Id>
<Type>0</Type>
<Icon>---</Icon>
<Title>Btn2Item4</Title>
</Item>
</p>
</Level>
in above code there are only 2 tag for <p>. but in actual there are multiple tag. i want to search the specific tag for which attribute id have some specific value (say 327).
so one way is that i parse the XML file from start to get the desired result. whether there are any other method from which i can direct locate the desired tag. for example if i want to search the <p> tag in above XML for attribute id =328, then it does not parse the id=327 and direct return only those item which are related to id=328
Please suggest

Depends how you define "parse".
A "quick & dirty" (and potentially buggy) way would be to find the fragment using a regex search (or a custom parser) first, then feed the fragment to a true XML parser. I don't know of anything that would do this for you, you'd have to roll it yourself. I would suggest that it's not the way to go.
The next level is to feed it through a SAX-like parser (which NSXMLParser is a form of).
In your handler for the <p> element, check the id attribute and if it matches your value (or values), set a flag to indicate if child elements should be interpreted.
In your child element handlers, just check that flag first (in a raw NSXMLParser handler all elements would go to the same method, of course).
So it's true that NSXMLParser would be parsing the whole document - but just to do the minimal work to establish the correct XML parser context. The real work of handling the elements would be deferred until the value is met. I don't see any way around that without something hacky like the regex suggestion.
If this is too much overhead I'd reconsider whether XML is the right serialization format for you (assuming you have any control over that)?
If you do stick with NSXMLParser, my blog article here might help to at least make the experience nicer.

The libxml2 library can receive XPath queries with the following extensions. With these extensions you might issue the XPath query /p[#id = "328"] to retrieve that specific node's children.

Related

Need to find the tags under a tag in an XML using jQuery

I have this xml as part of the responseXml of an Ajax call:
<banner-ad>
<title><span style="color:#ffff00;"><strong>Title</strong></span></title>
</banner-ad>
When I used this jQuery(responseXml).find("title").text(); the result is "Title".
I also tried jQuery(responseXml).find("title:first-child") but the result is [object Object].
I want to get the result:
<span style="color:#ffff00;"><strong>Title</strong></span>
Please let me know how to do this in jQuery.
Thanks in advance for any help.
Regards,
Racs
Your problem is that you cannot simply append nodes from one document (the XML response) to another (your HTML page). The issue is two-fold:
You can use jQuery to append nodes from the XML document to the HTML page. This works; the nodes appear in the HTML DOM, but they stay XML nodes and therefore the browser ignores the style attribute, for example. Consequently the text will not be yellow (#ffff00).
As far as I can see, jQuery offers no built-in way to get the XML string (i.e. a serialized node) from an XML node. jQuery can handle XML documents quite well, but there is no equivalent to what .html() does in HTML documents.
So to make this work we need to extract the XML string from the XML document. Some browsers support the .xml property on XML nodes (namely, IE), the others come with an XMLSerializer object:
// find the proper XML node
var $title = $(doc).find("title");
// either use .xml or, when unavailable, an XMLSerializer
var html = $title[0].xml || (new XMLSerializer()).serializeToString($title[0]);
// result:
// '<title><span style="color:#ffff00;"><strong>Title</strong></span></title>'
Then we have to feed this HTML string to jQuery so new, real HTML elements can be created from it:
$("#target").append(html);
There is a fiddle to show this in action: http://jsfiddle.net/Tomalak/QWHj8/. This example also gets rid of the superfluous <title> element.
Anyway. If you have a chance to influence the XML itself, it would make sense to change it:
<banner-ad>
<title><span style="color:#ffff00;"><strong>Title</strong></span></title>
</banner-ad>
Just XML-encode the payload of <title> and you can do this in jQuery:
$("#target").append( $(doc).find("title").text() );
This would probably work:
$(responseXml).find("title").html();

how to add new element to the existing node in xml

i have an xml file like this.
<?xml version="1.0" encoding="UTF-8"?>
<imageset>
<category>
<image>SQUIRREL</image>
<image>FOX</image>
<image>TIGER</image>
<image>LION</image>
</category>
</imageset>
Now i need to add another elemnt to the parent node imageset like this.
eg: i need to add <image>DOG</image>
SQUIRREL
FOX
TIGER
LION
DOG
i know that to get elements by node wise we need to use xml parse.
But i don't know to write element to the same xml file.
How can i done can any one please help me.
Thank u in advance.
If you are on iPhone you can use either a SAX parser called The "NSXMLParser" class
or you can use a DOM parser Like NSXML for this one You want to use DOM Like NSXML since you want to edit the file. On iPhone they Took NSXMLDocument out due to it being too resource intensive. Use something like either "Kiss XML" or "Touch Base XML".
If you are not familier with parsing XML its really simple and easy I suggest since you are on the iPhone use the ADC Documentation here:
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/NSXML_Concepts/NSXML.html
Its a great tutorial!

NSXMLParser with multiple delegates

I'm trying to parse an xml tree file whith multiple NSXMLParserDelegate parsers but I'm getting in the next issue.
My XML structure is something like that.
<Object1>
<Name>Ricky</Name>
<Surname>Woodstock</Surname>
<Adress>
<City>Los Angeles</City>
<State>California</State>
<Country>USA</Country>
</Adress>
<Items>
<Item>
<Id>1</Id>
<Description>Sports Bag</Description>
<Price>13.45</Price>
</Item>
<Item>
<Id>2</Id>
<Description>Baseball Cap</Description>
<Price>6.90</Price>
</Item>
</Items>
<Total>20.15</Total>
</Object1>
And my Issue is that when I changes delegates, it starts parsing not in top-level tag, instead it begins parsing on the first nested tag.
For example.
I begin parsing XML wih XMLObject parser which parses element
When it reaches Adresss tag I set delegate to XMLAdressParser (and set XMLAdressParser's parent to self for returning) but XMLAdressParser begins parsing on tag.
And almost with Items and Item tags.
I think it's normal cause of a Parser definition, but that's the question:
Is there any way for the delegate to start parsing in correspondint tag, like first one?
Thanks in advance for any help.
Sergio
Rather than using multiple delegates I suggest using a single delegate and having it distribute the work as you feel appropriate.

Ignore CDATA while xml parsing

I am new to iphone development.I want to ignore CDATA tag while parsing because it consider the HTML tag following it as text.Since i want to display the content alone ,i want my parser to ignore CDATA tag.My source code is
[CDATA[<br /><p class="author"><span class="by">By: </span>By Sydney Ember</p><br><p>In the week since an </p>]].
Is there any way to ignore CDATA tag?
Is there any way to parse my source twice so it displays only the content?
Please give me some sample code.Please help me out.Thanks.
If you treat the CDATA content as XML instead of CDATA then your parser will throw an error (since your HTML is a weird mix of XHTML and HTML and is not well formed).
If you want to get the HTML, then parse the XML, extract the text content of the node, then parse that text as HTML.
There is no way to ignore the CDATA tag - it's part of the xml spec and parsers should honour it.
If you don't like the idea of this answer to your earlier question, you could get the contents of the CDATA section and parse it as XML again. However, this is highly not recommended! You don't know that the contents of the CDATA are going to be valid xml (they're probably not).
If you can 100% guarentee that the CDATA section contains the form you have above, you could probably use some string manipulation to get the data out (i.e. string replace '<span class="by">By: </span>' with '') but again, this will almost certainly break if the CDATA contents change.
Where is the xml coming from? It's a better idea to talk to owner of the service and get them to send you instead of description something like
<description>
<author>By Sydney Ember</autho>
<text>In the week since an </text>
</description>
S

Gracefully Halting NSXMLParser?

still new to XML parsing with the iphone so i have a few questions.
in my opinion, id like my iphone-app to have to request out to the internet as less as possible, so i have combined a few XMLs i had to a single, larger one. However when im parsing information out, it has some similar node-names (not at the same level obviously) however its still causing me issues when trying to parse out my custom objects.
so should i just keep my XML all combined and rename some nodes? or make 2 XML calls?
opinions?
You need to manually track the state of your tree as you parse. The parser will work through your document in sequence so if you have an XML structure like so:
<a>
<name>some text</name>
<b>
<name>some other text</name>
</b>
</a>
You need to keep track of when your "a" and "b" elements start and end to know what context you are in.