SimpleXml Query

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • php_Boi

    SimpleXml Query

    i have a document with this structure. that is loaded from a file with
    the name $url.
    //valid xml format
    <Trip>
    <Item>
    <Type>Trans</Type>
    <Code>KAP</Code>
    <Date>2006030 1</Date>
    </Item>
    <Item>
    <Type>Trans</Type>
    <Code>BTL</Code>
    <Date>2006030 1</Date>
    </Item>
    <Item>
    <Type>Sight</Type>
    <Code>HTL</Code>
    <Date>2006030 1</Date>
    </Item>
    </Trip>
    Now to get the values of all the items i can do simply enough with

    foreach ($url>Trip->Item as $transfer)
    {
    $vType = $transfer->Type;
    $vCode = $transfer->Code;
    }

    But i need to perform a slightly more complex query.

    i need to get the the <Codeand the <Dateonly where the <Typeis
    "Sight"

    tried this but didnt have any luck. (casted as a string so tag wasnt
    assumed to be an object)
    if ((string) $url->Code == 'Sight') {
    print 'Success.';
    }
    else if((string) $url->Code == 'Trans') {
    print 'Success.';
    }
    else print 'Oh no!';

    which will recognize the first node but ignore the ' else if ' (the
    node where <Code>Trans</CodeDOES Exist ) and the ' else '???

  • php_Boi

    #2
    Re: SimpleXml Query

    On Jan 31, 5:47 pm, "php_Boi" <timan...@gmail .comwrote:
    i have a document with this structure. that is loaded from a file with
    the name $url.
    //valid xml format
    <Trip>
    <Item>
    <Type>Trans</Type>
    <Code>KAP</Code>
    <Date>2006030 1</Date>
    </Item>
    <Item>
    <Type>Trans</Type>
    <Code>BTL</Code>
    <Date>2006030 1</Date>
    </Item>
    <Item>
    <Type>Sight</Type>
    <Code>HTL</Code>
    <Date>2006030 1</Date>
    </Item>
    </Trip>
    Now to get the values of all the items i can do simply enough with
    >
    foreach ($url>Trip->Item as $transfer)
    {
    $vType = $transfer->Type;
    $vCode = $transfer->Code;
    >
    }
    >
    But i need to perform a slightly more complex query.
    >
    i need to get the the <Codeand the <Dateonly where the <Typeis
    "Sight"
    >
    tried this but didnt have any luck. (casted as a string so tag wasnt
    assumed to be an object)
    if ((string) $url->Code == 'Sight') {
    print 'Success.';}
    >
    else if((string) $url->Code == 'Trans') {
    print 'Success.';}
    >
    else print 'Oh no!';
    >
    which will recognize the first node but ignore the ' else if ' (the
    node where <Code>Trans</CodeDOES Exist ) and the ' else '???
    Appology in Advance, typo i meant
    if ((string) $url->Code == 'Sight') {
    print 'Success.';}

    else if((string) $url->Code == 'Trans') {
    print 'Failure.';}

    else print 'Oh no!';

    Comment

    Working...