PHP and xml help!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • CS-Jay
    Confirmed User
    • Oct 2003
    • 1794

    #1

    PHP and xml help!

    I'm a bit stuck and hope someone can lend a hand. I cannot get the value of, Choice, using simplexml.

    Here's the xml:

    PHP Code:
    <Choice ID="1000">
    <Select NAME="BOB/>
    Monday
    </Choice>
    <Choice ID="1001">
    <Select NAME="Mary/>
    Tuesday
    </Choice> 
    
    I am looping through, trying to get the value of Choice, but I get the array of Select. I would like just get the days.

    PHP Code:
    $arr = array();
    foreach ($Choice AS $choice){
    $arr[] = $choice;
    }
    print_r($arr); 
    
    Any ideas?
    Last edited by CS-Jay; 02-23-2011, 06:36 AM.
    I do stuff - aIm CS_Jay_D
  • potter
    Confirmed User
    • Dec 2004
    • 6559

    #2
    You're going to want an XML parser to parse the XML into an array.

    http://php.net/manual/en/function.xml-parse.php

    http://php.net/manual/en/function.xm...nto-struct.php

    Comment

    • CS-Jay
      Confirmed User
      • Oct 2003
      • 1794

      #3
      Originally posted by potter
      You're going to want an XML parser to parse the XML into an array.

      http://php.net/manual/en/function.xml-parse.php

      http://php.net/manual/en/function.xm...nto-struct.php
      I guess I should stop resisting using anything else than simplexml.
      I do stuff - aIm CS_Jay_D

      Comment

      • HarryMuff
        Confirmed User
        • Dec 2005
        • 271

        #4
        Use DOM? Don't think you can get the textNode children using simplexml.

        Code:
        $xml =
        '<derp>
            <Choice ID="1000">
                <Select NAME="BOB" />
                Monday
            </Choice>
            <Choice ID="1001">
                <Select NAME="Mary" />
                Tuesday
            </Choice>
        </derp>';
        $dom = new DOMDocument('1.0', 'UTF-8');
        $dom->loadXML($xml);
        foreach($dom->getElementsByTagName('Choice') as $choice)
        {
            $arr[] = $choice->textContent;
        }
        print_r($arr);

        Comment

        • CS-Jay
          Confirmed User
          • Oct 2003
          • 1794

          #5
          Originally posted by HarryMuff
          Use DOM? Don't think you can get the textNode children using simplexml.

          Code:
          $xml =
          '<derp>
              <Choice ID="1000">
                  <Select NAME="BOB" />
                  Monday
              </Choice>
              <Choice ID="1001">
                  <Select NAME="Mary" />
                  Tuesday
              </Choice>
          </derp>';
          $dom = new DOMDocument('1.0', 'UTF-8');
          $dom->loadXML($xml);
          foreach($dom->getElementsByTagName('Choice') as $choice)
          {
              $arr[] = $choice->textContent;
          }
          print_r($arr);
          Off to give DOM a try
          I do stuff - aIm CS_Jay_D

          Comment

          Working...