NSXMLParser with multiple delegates

2019-08-30 10:05发布

问题:

I'm trying to parse an xml tree file whith multiple NSXMLParserDelegate parsers but I'm getting in the next issue.

My XML structure is something like that.

<Object1>
     <Name>Ricky</Name>
     <Surname>Woodstock</Surname>
     <Adress>
          <City>Los Angeles</City>
          <State>California</State>
          <Country>USA</Country>
     </Adress>
     <Items>
          <Item>
               <Id>1</Id>
               <Description>Sports Bag</Description>
               <Price>13.45</Price>
          </Item>
          <Item>
               <Id>2</Id>
               <Description>Baseball Cap</Description>
               <Price>6.90</Price>
          </Item>
     </Items>
     <Total>20.15</Total>
</Object1>

And my Issue is that when I changes delegates, it starts parsing not in top-level tag, instead it begins parsing on the first nested tag.

For example.

  1. I begin parsing XML wih XMLObject parser which parses element

  2. When it reaches Adresss tag I set delegate to XMLAdressParser (and set XMLAdressParser's parent to self for returning) but XMLAdressParser begins parsing on tag.

  3. And almost with Items and Item tags.

I think it's normal cause of a Parser definition, but that's the question:

Is there any way for the delegate to start parsing in correspondint tag, like first one?

Thanks in advance for any help.

Sergio

回答1:

Rather than using multiple delegates I suggest using a single delegate and having it distribute the work as you feel appropriate.