need help parsing xml

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ghosttr
Forum Newbie
Posts: 1
Joined: Fri Feb 26, 2010 11:06 pm

need help parsing xml

Post by ghosttr »

Ive been using php to parse some xml but ive run across a problem.

For the most part the xml files that i get are formatted like this

For this

Code: Select all

Session xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
  <ret_cd>0</ret_cd> 
  <ret_msg>OK</ret_msg> 
  <session_id>-session id</session_id> 
  <timestamp>2/27/2010 4:56:10 AM</timestamp> 
  </Session>
 
i use something like this

Code: Select all

 
class API_SetSession
{
    function RETCD()
    {
        return $this->RETCD_;
    }
    function RETMSG()
    {
        return $this->RETMSG_;
    }
    function SESSIONID()
    {
        return $this->SESSIONID_;
    }
    function TIMESTAMP()
    {
        return $this->TIMESTAMP_;
    }
    function Fetch_XML()
    {
        //get the xml
    
    function startElement($parser, $name, $attribs)
    {
    // nothing to do here...
    }
    
    function endElement($parser, $name)
    {
     global $tempvalue;
 
        if ($name == "RET_CD")
            $this->RETCD_ = $tempvalue;
            
        if ($name == "RET_MSG")
            $this->RETMSG_ = $tempvalue;
            
        if ($name == "SESSION_ID")
            $this->SESSIONID_ = $tempvalue;
            
        if ($name == "TIMESTAMP")
            $this->TIMESTAMP_ = $tempvalue;
    }
    
    function characterData($parser, $data)
    {
      global $tempvalue;
 
      $tempvalue = $data;
    }
}
 
this works, and i can also work with it if there are multiple comma separated values in one of the elements.

like if the xml is like this

Code: Select all

 
<result>
- <rowset name="name" key="a" columns="a,b,c,d,e,f">
  <row AAAA a="1" b="1" c="1" d="1" e="1" f="1" /> 
  <row BBBB a="2" b="2" c="2" d="2" e="2" f="2" /> 
</result>
 
In this case each row is stored in an array.

Recently we got a new addition to the api, but it doesnt work in a way any of the previous 2 do.

It is formatted like this

Code: Select all

 
<result>
<user>
<username>aaaaaaa</username>
<userid>111111</userid>
</user>
<user>
<username>bbbbbb</username>
<userid>222222</userid>
</user>
</result>
 
I want to have this come out in an array like the comma separated ones do, but it wont do anything with the user/userid rows.

Heres what ive tried (and is not working =( )

Code: Select all

 
        
    function getRefTypes()
    {
        return $this->RefTypes_;
    }
..
 
function endElement($parser, $name)...
 
if ($name == "ROW")
        {
            $this->RefTypes_[] = $RefTypeData;
            $RefTypeData = array();
            unset($RefTypeData);
        }
 
With the comma seperated ones it will come out as an array. But when i try to put them in an array like this nothing will come out =(
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

Re: need help parsing xml

Post by yacahuma »

did you try SimpleXMLElement?
Post Reply