2012-02-28 90 views
0

我一直在试图让id字段拉,并不知道我要出错的地方。其余的数据正确拉动,但我试图添加一些新的一些现有的代码,我试过的一切都没有奏效。以下是我的XML和我一直在努力的PHP代码。从一个xml上传中拉出一个特定的字段

我以前没有用过xml和php的组合,所以我真的可以在正确的方向上使用push。

<?xml version="1.0" encoding="UTF-8"?> 
<enterprise> 
    <person> 
     <sourcedid> 
      <source>Spirit Awards</source> 
      <id>SP8675309</id> 
     </sourcedid> 
     <userid>...</userid> 
     <name> 
      <fn>...</fn> 
     </name> 
     <email>...</email> 
    </person> 

PHP代码:

function get_userid(){ 
    return $this->uid; 
} 
function __construct($xmlData){ 
    $this->uid = (string)$xmlData->id; 
} 
+0

另外,欢迎使用Stack Overflow。 – rdlowrey 2012-02-28 03:00:41

回答

0

SimpleXMLdocs使得这个......嗯......简单。您发布的XML缺少关闭</enterprise>标签。假设您实际解析的XML包含结束标记,请考虑:

$str = '<?xml version="1.0" encoding="UTF-8"?> 
<enterprise> 
    <person> 
     <sourcedid> 
      <source>Spirit Awards</source> 
      <id>SP8675309</id> 
     </sourcedid> 
     <userid>...</userid> 
     <name> 
      <fn>...</fn> 
     </name> 
     <email>...</email> 
    </person> 
</enterprise> 
'; 

$xml = simplexml_load_string($str); 
$var = (string) $xml->person->sourcedid->id; 
echo $var; // outputs: SP8675309 
+0

是的,我刚刚复制了文件的一部分。它确实有一个结束标记,我只是复制了一部分文件,用于上传到页面的示例。我现在要试一试,看看我能否实现它。谢谢! – Anirok 2012-02-28 04:24:19

相关问题