views:

150

answers:

2

hi all, 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

A: 

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.

Phil Nash
A: 

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.

Alex Reynolds
That will still parse the whole document, though, won't it (at least up until the required node)
Phil Nash