PHP解析XML字符串问题

PHP解析xml有以下几种方法:
1.simplexml_load_string.
2.DOMDocument.
3.XMLReader.

$xml = <<  
XML; 

$x = simplexml_load_string($xml); 
print_r($x); 
/*
out: 
SimpleXMLElement Object 
( 
    [@attributes] => Array 
        ( 
            [type] => noteRequest 
            [detail] => onlyTitle 
            [username] => 001 
            [password] => 
            [fileID] => 1 
        ) 

    [0] =>  
)
*/

foreach($xml->attributes() as $name => $value) { 
    echo $name,'="',$value,"\"\n"; 
}

  $dom = new DOMDocument();
  $dom->loadXML($string);
  $xml_value = $dom->getElementsByTagName('rsp');//标签
  foreach($xml_value as $value){
    $nodevalue = $value->nodeValue;
  }


$xml ="1.xml";   
$dom = new DOMDocument();   
$dom->load($xml);   
$root = $dom->documentElement;   
$arr=array();   
foreach ($root->childNodes as $item)   
{   
    if($item->hasChildNodes()){   
  
$tmp=array();   
    foreach($item->childNodes as $one){   
        if(!emptyempty($one->tagName)){   
        $tmp[$one->tagName]=$one->nodeValue;   
        }   
    }   
    $arr[$item->tagName]=$tmp;   
    }   
}   
print_r($arr);   

相关资料:
http://www.ibm.com/developerworks/cn/xml/x-simplexml.html

发表评论

返回顶部