How to parse xml file for cocos2D - objective-c

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.

Related

How to use relative or absolute file path in Karate.write function

I am trying to write my contents into an external file. How to define your file path in the Karate.write(value, file) ?? (I am aware that it is not recommended from the framework perspective but I have to do some JMS activity with my customised Java functions. That is why I am trying to do this)
Anyone coming across this answer in the future, please read this first: https://stackoverflow.com/a/54593057/143475
Even if you need to do JMS, I don't understand why you need to write a file. Read the above link again if you need to please. Also Karate has support for async and we even have a JMS example here: https://github.com/intuit/karate/tree/master/karate-netty#consumer-provider-example
Finally: my strong advice to you: write a Java or JS utility yourself. Look at this example for hints: upload.feature especially this part:
* def FileChecker = Java.type('com.intuit.karate.demo.util.FileChecker')
# example of parsing a string into json by karate
* json fileInfo = FileChecker.getMetadata(id)
Here we are reading a file, but you get the idea.

Difference between TouchXML and GDataXML parser

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.

Objective C, How to parse local XML file in Resources folder?

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.

dojo js library + jsdoc -> how to document the code?

I'd love to ask you how do the guys developing dojo create the documentation?
From nightly builds you can get the uncompressed js files with all the comments, and I'm sure there is some kind documenting script that will generate some html or xml out of it.
I guess they use jsdoc as this can be found in their utils folder, but I have no idea on how to use it. jsDoc toolkit uses different /**commenting**/ notations than the original dojo files.
Thanks for all your help
It's all done with a custom PHP parser and Drupal. If you look in util/docscripts/README and util/jsdoc/INSTALL you can get all the gory details about how to generate the docs.
It's different than jsdoc-toolkit or JSDoc (as youv'e discovered).
FWIW, I'm using jsdoc-toolkit as it's much easier to generate static HTML and there's lots of documentation about the tags on the google code page.
Also, just to be clear, I don't develop dojo itself. I just use it a lot at work.
There are two parts to the "dojo jsdoc" process. There is a parser, written in PHP, which generates xml and/or json of the entirety of listed namespaces (defined in util/docscripts/modules, so you can add your own namespaces. There are basic usage instructions atop the file "generate.php") and a Drupal part called "jsdoc" which installs as a drupal module/plugin/whatever.
The Drupal aspect of it is just Dojo's basic view of this data. A well-crafted XSLT or something to iterate over the json and produce html would work just the same, though neither of these are provided by default (would love a contribution!). I shy away from the Drupal bit myself, though it has been running on api.dojotoolkit.org for some time now.
The doc parser is exposed so that you may use its inspection capabilities to write your own custom output as well. I use it to generate the Komodo .cix code completion in a [rather sloppy] PHP file util/docscripts/makeCix.php, which dumps information as found into an XML doc crafted to match the spec there. This could be modified to generate any kind of output you chose with a little finagling.
The doc syntax is all defined on the style guideline page:
http://dojotoolkit.org/reference-guide/developer/styleguide.html

Objective-C libraries for XML Parsing

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.