I have a XML file in resources folder in xcode and I want to parse the XML..
The question is how to get access to the file programmatically?
Thanks in advance.
NSXMLParser is an event driven xml parser. You can use it but I tend to like query based parsers better. Lib2Xml.
this will get you started with lib2xml
http://cocoawithlove.com/2008/10/using-libxml2-for-parsing-and-xpath.html
An xml file is the just another text file. This has info on different ways to load xml files.
How to save/load text files in Objective-C for the iPhone, using UITextView?
There are many options. Two of the most popular: use Cocoa's NSXMLParser class, or use TouchXML. If you use NSXMLParser, you'll end up creating a delegate for the parser that decides what to do whenever the parser encounters XML tags, data, etc.
Related
I need to read the .txt file as raw text or by code to extract the data keyed in the test suite (resource/assertors,....). Is there any way to do that? by code or any editor.
If you have binary format of tst file then there could be a problem, there is no official API to read it.
It's very old format, I don't think that is still in use.
There could be also two, newest, formats of tst:
compressed XML
XML
In case of compressed XML you have to unzip it and then you have access to XML, where you can read it as text file.
In case of XML, it's just XML, you can read it as pure text file.
There is no official API which allows to read it in similar way as SOAtest's GUI to use in code i.e.: in Java.
I am trying to parse a xml document in my resource folder in my project. The project is a cocos2D project and i am trying to create some type of method to parse the xml file to load levels for my game.
What would be the best way to parse the document? And an example would be great.
You can use any Objective-c XML parser in cocos2d
I suggest you to use TBXML
But instead of having data in your xml, you should save it into plist and use it directly.
If
1) the speed of parsing and
2) the convenience of retrieving the data from xml file is important
3) you do not mind C++ interface
then the best parser for you is open source:
http://code.google.com/p/pugixml/
pugixml is a light-weight C++ XML processing library. It features:
• DOM-like interface with rich traversal/modification capabilities
• Extremely fast non-validating XML parser which constructs the DOM tree from an XML file/buffer
• XPath 1.0 implementation for complex data-driven tree queries
• Full Unicode support with Unicode interface variants and automatic encoding conversions
The library is extremely portable and easy to integrate and use.
I have tested both speed and memory usage against other parsers and use it for last 5 years.
Documentation is here: http://pugixml.googlecode.com/svn/tags/latest/docs/quickstart.html
Even the post is old, I write the solution here to help other people like me in future. Solution is simple:
- Step 1: Read file content by using FileUtils
- Step 2: Parse file content to tinyxml2 (supported
I have tried and success on loading and reading XML file in cocos2d 3.8
#include "tinyxml2/tinyxml2.h"
auto s = FileUtils::getInstance()->getStringFromFile("data.xml");
tinyxml2::XMLDocument *pDoc = new tinyxml2::XMLDocument();
tinyxml2::XMLError errorId = pDoc->Parse(s.c_str(), s.size());
if (errorId == 0) {
// Write your code here
}
data.xml is placed in Resource folder.
Please read http://www.cocos2d-x.org/docs/manual/framework/native/v3/xml-parse/zhparse/zh to know how to iterate tinyxml2 XMLDocument.
Note that you can not use the tutorial in this link to load from file. It always return ERROR_FILE_NOT_FOUND.
I have two options in front of me for parsing really fat XML file,
TouchXML
GDataXML
It's lot of work to do because XML file is very huge. I thought of asking people who have already worked with these parsers.
Which one is better for fat XML files?
I found a blog post which says that TouchXML does not edit/save XML files whereas GDataXML has that feature. What exactly do they mean by edit/save XML file feature?
Lets see if I can answer your questions:
Which one is better for fat XML files? The answer is neither. Both are DOM parsers, which actually load the entire document into memory to make queries faster. If you're parsing a large file, you're better off going with a SAX parser, such as the built-in NSXMLParser, or even the SAX-based version of libxml2.
What exactly do they mean by edit/save XML file feature? Well, suppose you have a XML file that has your app's settings in it. If you open up that file and make changes, you're going to want to save them, right? That's where the writing comes in. The parsers that allow writing let you save the representation of the xml file in the memory into an actual file that can be written to disk.
I'm working on a project for the iPad, I need to read and write to an xml file, which is also used by the counter part of the application in windows.
The problem that I have is that I've been looking around but I haven't found a way to modify an element or attribute in an xml, without having to build the whole xml again.
I saw this other post, which is basically the same problem that I have, and I also end it up in the same point as the person asking the question, NSXMLParser and TouchXML are read only and do not allowed me to modify my xml.
Any other suggestion about what can I use?
Thanks!
I would like to know some libraries in objective-C for xml parsing. I think it is a very common need, but I found limited resources for handling this task:
Google Code projects: TouchCode (TouchXML)
NSXMLParser
What is your best solution to work with XML in objective-C language? Please advice.
What is the solution that you have used for your product?
NSXMLParser is a stream-oriented class; you set it up and get delegate callbacks when it detects something. Usually this is not what you want to do, but can be much faster and lower memory.
TouchXML will parse the XML itself using libxml, and create an object tree for the entire XML structure. This allows you to easily access the contents of the XML tree, using manual traversal methods or basic XPaths (more sophisticated XPath support is planned).
It serves a narrow purpose, but if your goal is to parse untidy HTML, you might want to try a static library I started called TagScraper. It doesn't handle many/most XML/HTML entities correctly, but it could be pretty easily patched to. URL: http://github.com/searls/TagScraper
Its value is that it provides a simple XPath mechanism that hides the tidying/querying/assembling for you, and then it provides the parsed elements & attributes in a tree-like data structure of Tag.h nodes.