0

Using NSXMLDocument, without using XPath, is there an easy way to parse an XML document and deserialize it into custom objects to create an object tree hierarchy?

For example, if I have the xml shown below, is it possible to put the details into a Restaurant object and a content object within it?

<restaurants>
   <content>spanish name</content>
   <content>english name</content>
</restaurant>
<spa>
   <content>spa spanish name</content>
   <content>spa english name</content>
</spa>

I will be using your answer above to extend it for programming in kissxml in iOS. As the kissXML document mentions that the XML parser behaves in the same way as NSXMLDocument, so I've asked the question using NSXMLDocument.

uchuugaka
  • 12,679
  • 6
  • 37
  • 55
inforeqd
  • 3,209
  • 6
  • 32
  • 46

1 Answers1

1

If you know your expected content structure, the easiest is to just use NXMLParser and loop through it looking for the bits you need and keeping track of the previous bit, building an object as you find them.

If you want tree-based approach, consider learning XQuery and XPath, they are not all that bad. Without them, the only thing NSXMLDocument really gives you is Cocoa bindings.

At the end of the day you must transform your data somehow.

With NSXMLDocument you will still do well to validate against an XML DTD if possible, to ensure you have good data.

With NSXMLParser, you are able to handle things without a formal DTD. You only need worry about how big the data is, an how you want to parse it, then do some trial and error with test data to ensure it's grabbing what you want or need.

uchuugaka
  • 12,679
  • 6
  • 37
  • 55