how to add new element to the existing node in xml - iphone

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!

Related

Get the tileset file names for TMXTiledMap

What's the best way to retrieve an array containing the file names of the files used to load a certain CCTMXTiledMap ? I need to get the tileset file names because I want to dispose of them personally from the Cache in a future.
According to the documentation and declaration of CCTMXTiledMap, you'll want to walk the property NSMutableArray* objectGroups. Otherwise, you'll need to maintain your own data structure.
This thread contains robust documentation and examples that should help you.
Open the tmx file, it is an xml file, and search for
it has a tag with a source attribute.
<tileset firstgid="1" name="sewer_tileset" tilewidth="24"
tileheight="24"> <image source="sewer_tileset.png" trans="ff00ff"
width="192" height="217"/> </tileset>
you can parse the xml and just find all tileset->images.
You will need an xml parser if you want to do it on the phone, here is a good tutorial on that :http://www.raywenderlich.com/553/how-to-chose-the-best-xml-parser-for-your-iphone-project

How to add a node to an existing xml file using iphone?

In my application I need to add new element to parent node.
for Example My Xmlfile:
<person>
<Name>Thrinath</Name>
<Add>Hyd</Add>
</person>
Now I need to add new element named sal to person parent node
<person>
<Name>Thrinath</Name>
<Sal>50000</Sal>
<Add>Hyd</Add>
</person>
I know that to read XML file we have XML parser,but I dont know how to write into the XML file like my requirement.
Use some kind of DOM model to modify the XML, see for example this link.

How to parse XML in objective c

Hello i want to parse xml.Please help me.Here is my node
<RadioButton id="1" name="Gender">
<Item>Male</Item>
<Item>Female</Item>
</RadioButton>
i want to parse and i want to create comma separated string
Here are releated the SO post,
Parser XML with NSXMLParser
http://www.raywenderlich.com/553/how-to-chose-the-best-xml-parser-for-your-iphone-project
NSXMLParser example
below is the blog tutorial for using NSXMLParser.
http://markstruzinski.com/?p=47
http://sivatechranti.wordpress.com/2010/03/05/parsing-xml-on-iphone/
easy to explain
anonter site very useful
http://mac.softpedia.com/progDownload/TouchXML-Download-47225.html
direct download...and see this project and implemant in yr project and drag in your project and use directly.....

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

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.

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.