SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Jun 2009
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help to get XML child nodes in php

    Hi All,

    I'm new to PHP. I need a help from you.

    I'm stuck on pulling in an xml child node as a value

    I have tags in xml like the below.

    <groups>
    <Group1>
    <GroupName>Group1</GroupName>
    <Items>
    <item1>
    <item>l1</item>
    <feedback>feed1.png</feedback>
    <Additional_info_question>feed1</Additional_info_question>
    </item1>
    <item2>
    <item>l2</item>
    <feedback>fl2</feedback>
    <Additional_info_question>No info</Additional_info_question>
    </item2>
    </Items>
    </Group1>
    <Group2>
    <GroupName>Group2</GroupName>
    <Items>
    <item1>
    <item>r1</item>
    <feedback>fr1</feedback>
    <Additional_info_question>No info</Additional_info_question>
    </item1>
    <item2>
    <item>r2</item>
    <feedback>fr2</feedback>
    <Additional_info_question>No info</Additional_info_question>
    </item2>
    <item3>
    <item>r3image.png</item>
    <feedback>fr3</feedback>
    <Additional_info_question>No info</Additional_info_question>
    </item3>
    </Items>
    </Group2>
    </groups>

    How can I get the values from <item>, <feedback> and <Additional_info_question> tags.

    Please help me.

    Thanks in advance.
    Guru

  2. #2
    SitePoint Guru
    Join Date
    Feb 2008
    Posts
    655
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's a bit hard to read your XML as it isn't indented or colour coded very helpfully.

    You need to load the entire XML string using simplexml_load_string(). It will convert the entire thing to an object and make it very simple to work with.

    Groups will the be an array and within each of these arrays, Items will be an array.

    Try doing something like this and you will see how the data is structured and what is contained in all the various arrays.

    PHP Code:
    $data simplexml_load_string($string);
    echo 
    "<pre>";
    print_r($data);
    echo 
    "</pre>"
    Post again if you need any more specific advice.

  3. #3
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,398
    Mentioned
    65 Post(s)
    Tagged
    1 Thread(s)
    You could use SimpleXML as corbyboy suggested. To get at the relevant nodes is a simple task with XPath. Assuming your XML is in a string called $sXml:

    PHP Code:
    $oXml = new SimpleXMLElement($sXml);
    $aItems $oXml->xpath('//item/..');
    foreach (
    $aItems as $oItem)
    {
        
    printf(
            
    '<p>item: %s<br>feedback: %s<br>info: %s</p>',
            
    $oItem->item,
            
    $oItem->feedback,
            
    $oItem->Additional_info_question
        
    );

    The XPath there looks for item nodes and traverses to its parent node (in your XML, nodes like item1, item2). Then it's a matter of just looping over them and fetching the relevant child nodes.
    Salathe
    Software Developer and PHP Manual Author.

  4. #4
    SitePoint Member
    Join Date
    Jun 2009
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you very much all.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •