Simple XML Parsing in PHP4?

Discussion in 'PHP' started by Nick W, Oct 21, 2004.

  1. #1
    Hi all,

    I have an xml file like this:
    
    <container>
      <item>
        <val1>Some val</val1>
        <val2></val2>
        <val3>some other val</val3>	
        <val4>And yet another</val4>
      </item>
    </container>
    
    Code (markup):
    There are hundreds of items.

    What is the simplest functions to use to parse this so that i can get the values of <val1> and <val4>

    Im happy to go read the manual but i hoped someone might point me in the right direction to get me started?

    thanks...

    Nick
     
    Nick W, Oct 21, 2004 IP
  2. digitalpoint

    digitalpoint Overlord of no one Staff

    Messages:
    38,334
    Likes Received:
    2,613
    Best Answers:
    462
    Trophy Points:
    710
    Digital Goods:
    29
    #2
    Assuming your XML document is in the variable $data...

    $parser = xml_parser_create('UTF-8');
    xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1); 
    xml_parse_into_struct($parser, $data, $vals, $index); 
    xml_parser_free($parser);
    PHP:
    That will leave you with two arrays ($vals and $index) that will give you everything you need.
     
    digitalpoint, Oct 21, 2004 IP
    firemarsh likes this.
  3. Nick W

    Nick W Peon

    Messages:
    67
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #3
    >>xml_parse_into_struct

    it all comes flooding back.. sheesh i have no memory, i actually did somthing similar once lol!

    The only thing now is to try and get my head around the two array thing, i remember that being a real headache!

    thanks shawn, very much obliged mate :)

    Nick
     
    Nick W, Oct 21, 2004 IP
  4. TwisterMc

    TwisterMc Mac Guru

    Messages:
    972
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Oh I've wondered this for a while.
     
    TwisterMc, Oct 21, 2004 IP
  5. Ozz

    Ozz Peon

    Messages:
    112
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    If you have PEAR installed you can use the XML_SERIALIZER package.

    You can get a XML file into an array and work with the array as you like. I can provide yo uwith an example and I think you can find tutorials in the web.
     
    Ozz, Oct 25, 2004 IP
  6. roy77

    roy77 Active Member

    Messages:
    1,088
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    78
    #6
    there are some tutorials i saw online, if you cnt find the answer here (and sry i cnt help u :( ) i would suggest you search yahoo or google
     
    roy77, Jan 6, 2007 IP
  7. killer2021

    killer2021 Peon

    Messages:
    872
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #7
    very useful thank you!
     
    killer2021, Nov 9, 2008 IP
  8. Varsys Inc.

    Varsys Inc. Peon

    Messages:
    51
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Looks good!
     
    Varsys Inc., Nov 13, 2008 IP
  9. elladrone

    elladrone Peon

    Messages:
    116
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #9
    use PEAR, as already suggested. I do this for datafeeds in xml, works fine.
     
    elladrone, Dec 28, 2009 IP