0

I'm already known with TBXML on how to parse it in my Xcode project. But I'm stuck on an XML Structure I don't know well.

This is the XML Structure:

    <CurDate Dates="27.07.2012" Date="07/27/2012">
    <Currency Kod="USD" CurrencyCode="USD">
    <Unit>1</Unit>
    <Name>AMERICA</Name>
    <CurrencyName>US DOLLAR</CurrencyName>
    <ForexBuying>1.81</ForexBuying>
    <ForexSelling>1.8187</ForexSelling>
    </Currency>
    </CurDate>

I want help on this XML Structure. My code looks like:

        TBXMLElement *elementName = [TBXML childElementNamed:@"Currency" parentElement:element];
        TBXMLElement *altinTemp = [TBXML childElementNamed:@"CurrencyName" parentElement:elementName];

This is my way to get the CurrencyName of my XML, but I get an error on this. See code:

    + (TBXMLElement*) childElementNamed:(NSString*)aName parentElement:(TBXMLElement*)aParentXMLElement{
TBXMLElement * xmlElement = aParentXMLElement->firstChild;
const char * name = [aName cStringUsingEncoding:NSUTF8StringEncoding];
while (xmlElement) {
    if (strlen(xmlElement->name) == strlen(name) && memcmp(xmlElement->name,name,strlen(name)) == 0) {
        return xmlElement;
    }
    xmlElement = xmlElement->nextSibling;
}
return nil;
}

This is were I get an error. The error is "Thread 1: EXC_BAD_ACCESS (code=2, address=0x10)

Any reply will good for me! Thanks.

BlackJack
  • 11
  • 3
  • Please add the complete error message to your question. – Codo Jul 28 '12 at 18:57
  • I've added the error message above... – BlackJack Jul 28 '12 at 19:20
  • The modified question now shows the full error message. But it doesn't say anymore on which line it occurs. Anyway, it's a null pointer access. It means that the parameter *aParentXMLElement* is passed nil. So the real source of the error is even earlier. But from the information in your question, I can't tell. – Codo Jul 28 '12 at 22:04

1 Answers1

0

I believe you want something like this

NSError *error = nil;
TBXML* tbxml = [TBXML tbxmlWithXMLString:@"<CurDate Dates='27.07.2012' Date='07/27/2012'><Currency Kod='USD' CurrencyCode='USD'><Unit>1</Unit><Name>AMERICA</Name><CurrencyName>US DOLLAR</CurrencyName><ForexBuying>1.81</ForexBuying><ForexSelling>1.8187</ForexSelling></Currency></CurDate>" error:&error];

if (error) {
    NSLog(@"%@ %@", [error localizedDescription], [error userInfo]);
} else {
// If TBXML found a root node, process element and iterate all children
    if (tbxml.rootXMLElement){
        TBXMLElement *element = tbxml.rootXMLElement;

        if ([TBXML childElementNamed:@"Currency" parentElement:element]) {

            element = element->firstChild;

            do{
                TBXMLElement *altinTemp = [TBXML childElementNamed:@"CurrencyName" parentElement:element];
                NSString *currencyName = [TBXML textForElement:[TBXML childElementNamed:@"CurrencyName" parentElement:element]];
                NSLog(@"%@",currencyName);
            }while ((element = element->nextSibling));

            error = nil;
        } 
    }
}

A alternative to

childElementNamed: parentElement:

is

childElementNamed: parentElement: error:

Which allows you to deal with either nil nodes (which I presume you had). the error: can also be added to the majority of TBXML class functions it seems, it will cut performance so best to use it only for debugging or for times when the node/value may or may not be present.

Sam
  • 181
  • 1
  • 4