$str1 = "http://user.qzone.qq.com/1340477530#!app=2&pos=1300412602";
$xml = '<?xml version="1.0" encoding="utf-8"?>'."\r\n";
$xml .= '<user>'."\r\n";
$xml .= "\t".'<person_siteurl>'.$str1.'</person_siteurl>'."\r\n";
$xml .= '</user>'."\r\n";
$sxe = new SimpleXMLElement($xml);
$sxe->asXML('test.xml');运行后发现如下错误,arning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: Entity: line 3: parser error : EntityRef: expecting ';' in C:\wamp\www\test\a.php on line 18
Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: <person_siteurl>http://user.qzone.qq.com/1340477530#!app=2\&pos=1300412602</per in C:\wamp\www\test\a.php on line 18
将$str1中的 "&" 去掉后就正常了,这是为什么啊。。有什么解决办法吗?谢谢~~

解决方案 »

  1.   

    $xml .= "\t".'<person_siteurl>'.htmlentities($str1).'</person_siteurl>'."\r\n";
      

  2.   

    给个例子不知道对不对,各位拍下砖://xml string
    $xml_string="<?xml version='1.0'?>
    <users>
       <user id='398'>
          <name>Foo</name>
          <email>[email protected]</name>
       </user>
       <user id='867'>
          <name>Foobar</name>
          <email>[email protected]</name>
       </user>
    </users>"; 
    //load the xml string using simplexml
    $xml = simplexml_load_string($xml_string); 
    //loop through the each node of user
    foreach ($xml->user as $user)
    {
       //access attribute
       echo $user['id'], '  ';
       //subnodes are accessed by -> operator
       echo $user->name, '  ';
       echo $user->email, '<br />';
    }