Vijay
Vijay

Reputation: 579

how can i parse the element using attribute in xml parse in iphone?

dear friend i want parse the element using attributes in xml parser this is my xml format

<LOB>Cars</LOB>
<PPL>Indica</PPL>
<REGION>West2</REGION>
<RETAIL>30</RETAIL>
<LOB>Cars</LOB>
<PPL>Indica</PPL>
<REGION>West1</REGION>
<RETAIL>175</RETAIL>
<LOB>Cars</LOB>
<PPL>Indica Vista</PPL>
<REGION>West2</REGION>
<RETAIL>267</RETAIL>
<LOB>Cars</LOB>
<PPL>Indica Vista</PPL>
<REGION>West1</REGION>
<RETAIL>1212</RETAIL>

 more.....


<LOB>PCV - Venture</LOB>
<PPL>Venture</PPL>
<REGION>West2</REGION>
<RETAIL>2</RETAIL>
<LOB>PCV - Venture</LOB>
<PPL>Venture</PPL>
<REGION>West1</REGION>
<RETAIL>12</RETAIL>

 more....

now i want only cars retail count but here PCV-Venture attributes also in a same xml file so what can i do only for attribute is cars then parse retail element not for PCV - Venture's?

Upvotes: 1

Views: 119

Answers (2)

Rajneesh071
Rajneesh071

Reputation: 31081

Use TBXML parsing to parse xml format data.

link TBXML

data should be in this format

<info>
<User>
<CountryId>13</CountryId>
<CountryName>Austrlia</CountryName>
</User>
<User>
<CountryId>12</CountryId>
<CountryName>India</CountryName>
</User>
<User>
<CountryId>17</CountryId>
<CountryName>test1</CountryName>
</User>
<User>
<CountryId>16</CountryId>
<CountryName>UK</CountryName>
</User>
</info>  

and parse as

TBXML * tbxml = [TBXML tbxmlWithURL:[NSURL URLWithString:url]];

    TBXMLElement *rootXMLElement = tbxml.rootXMLElement;

    if (rootXMLElement)
    {
        TBXMLElement * user = [TBXML childElementNamed:@"user" parentElement:rootXMLElement];


        while (user != nil)
        {

            TBXMLElement *CountryId = [TBXML childElementNamed:@"CountryId" parentElement:user];
            if (CountryId != nil)
            {
                NSLog(@"CountryId :: %@", [TBXML textForElement:CountryId]);
            }

            TBXMLElement *CountryName = [TBXML childElementNamed:@"CountryName" parentElement:user];
            if (CountryName != nil)
            {
                NSLog(@"CountryName :: %@", [TBXML textForElement:CountryName]);
            }

            user = [TBXML nextSiblingNamed:@"user" searchFromElement:user];
        }

    }

Upvotes: 2

mkaracan
mkaracan

Reputation: 3

You can use SMXMLDocument.

It is an example

Upvotes: 0

Related Questions