繁体   English   中英

使用simpleXML进行XML解析

[英]XML parsing using simpleXML

我正在尝试解析页面上找到的XML ...

http://www.rapleaf.com/apidoc/person

Name: Test Dummy
Age: 42
gender: Male
Address: San Francisco, CA, US
Occupation:
University: Berkeley
first seen: 2006-02-23
last seen: 2008-09-25
Friends: 42
Name:
Age:
gender:
Address:
Occupation:
University:
first seen:
last seen:
Friends: 

1)我必须删除找到“&”的记录。 之后,我才能处理页面。

2)我无法解析“会员站点”,也无法解析“职业”

3)我只希望获得2条记录。

4)如何将这些记录插入数据库?

<?php

// displays all the file nodes
if(!$xml=simplexml_load_file('rapleaf.xml')){
    trigger_error('Error reading XML file',E_USER_ERROR);
}

foreach($xml as $user){
    echo 'Name: '.$user->name. '
<br /> Age: '.$user->age.'
<br /> gender: '.$user->gender.'
<br /> Address: '.$user->location.'
<br /> Occupation: '.$user->occupations->occupation->company.'
<br /> University: '.$user->universities->university.'
<br /> first seen: '.$user->earliest_known_activity.'
<br /> last seen: '.$user->latest_known_activity.'
<br /> Friends: '.$user->num_friends.'
<br />';
}

?>

为了能够解析该文档(格式不正确),我建议执行以下操作:

$xmlString = file_get_contents('rapleaf.xml');
$xmlString = str_replace('&', '&amp;', $xmlString);

if(!$xml=simplexml_load_string($xmlString)){
    trigger_error('Error reading XML file',E_USER_ERROR);
}

首先,将文件读取为字符串,并用其实体替换“&”字符(在链接内)。 您可以使用simplexml_load_file()函数创建xml对象。

现在您可以解析文档了。 据我所知,每个文件中只有一个人。 因此,您不需要foreach循环。 但是您可以解析所有字段,只需知道如何操作即可。 这是一些更复杂的示例,使用不同的方法解析不同的内容:

echo '    Name: '.(string)$xml->basics->name. '
        <br /> Age: '.(string)$xml->basics->age.'
        <br /> gender: '.(string)$xml->basics->gender.'
        <br /> Address: '.(string)$xml->basics->location;
// There might be more than one occupation
foreach($xml->occupations as $occupation){
    echo '<br /> Occupation: '.$occupation->attributes()->title;
    if(isset($occupation->attributes()->company)){
        echo '; at company: '.$occupation->attributes()->company;
    }
}
// There might be more than one university
foreach($xml->universities as $university){
    echo '<br /> University: '.$university;
}
echo    '<br /> first seen: '.(string)$xml->basics->earliest_known_activity.'
        <br /> last seen: '.(string)$xml->basics->latest_known_activity.'
        <br /> Friends: '.(string)$xml->basics->num_friends;
// getting all the primary membership pages
foreach($xml->memberships->primary->membership as $membership){
    if($membership->attributes()->exists == "true"){
        echo '<br />'.$membership->attributes()->site;
        if(isset($membership->attributes()->profile_url)){
            echo ' | '.$membership->attributes()->profile_url;
        }
        if(isset($membership->attributes()->num_friends)){
            echo ' | '.$membership->attributes()->num_friends;
        }
    }
}

对于标签中包含的文本,必须将其强制转换为字符串:

echo 'Name: '.(string)$xml->basics->name;

要获取标签属性的值,请使用attributes()函数。 您这次不必强制转换:

echo 'Occupation: '.$xml->occupations->occupation[0]->attributes()->title;

如您所见,您还可以获得一个特定的子节点,因为所有的子节点都存储在一个数组中。 只需使用索引。 如果只需要一个子节点,则不必为此使用循环。

但是您必须始终确保使用attirbutes()函数的元素有效,否则将引发错误。 因此,可能要通过isset()进行测试以确保确定。

我希望您现在对如何使用SimpleXML解析一些XML有一个想法。 如果您还有其他问题,只需再问一次,甚至再问一个新问题。

1。 “&”号是XML语法规范的一部分(用于编码非标准字符)。 因此,它们不能在XML文档中单独使用。 它们必须编码为&,或者必须包含在CDATA块中: http : //www.w3schools.com/xmL/xml_cdata.asp

2。 您不能访问这样的子元素($ user-> occupations-> occupation),因为该元素具有子元素。 您将必须执行以下操作:

$a = $user->occupations->children();
$b = $b->occupation->attributes();
$c = (string)$b->company;

查看http://php.net/manual/de/book.simplexml.php了解更多信息。

3。 您将获得两条记录,因为XML元素始终具有一个根元素,该根元素将其子元素括起来。 因此,当您遍历$ xml上的foreach时,首先会获得一个SimpleXMLElement对象,然后是。 用作根元素。

4。 这确实是另一个问题,并且取决于要使用的数据库。 Google会帮助您。 您可能要使用MySQL,因为您正在使用php。 因此,请查看http://www.google.de/search?sourceid=chrome&ie=UTF-8&q=php+mysql+tutorial :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM