如何从XML标记中获取元素以存储到PHP数组中?

时间:2017-10-25 09:58:26

标签: php arrays xml dom xpath

我搜索几天意味着在PHP中存储数组PHP,我的XML结构如下所示," URI "与他们各自的" SurfaceForm "。

相关联
  

寻求结构的例子:

[0] => "House Judiciary Committee" 
       [URI] =>"http://dbpedia.org/resource/United_States_House_Committee_on_the_Judiciary"

我的XML(= $ documentXML):

<?xml version="1.0" encoding="utf-8"?>
<Annotation text="The chairman of the House Judiciary Committee claims he obtained a “smoking gun” email that proves the Obama Justice Department prevented settlement payouts from going to conservative-leaning organizations, even as liberal groups were awarded money and DOJ officials denied “picking and choosing” recipients." confidence="0.35" support="0" types="" sparql="" policy="whitelist">
<Resources>
    <Resource URI="http://dbpedia.org/resource/United_States_House_Committee_on_the_Judiciary" support="805" types="" surfaceForm="House Judiciary Committee" offset="20" similarityScore="0.9999998407862576" percentageOfSecondRank="1.592137605044176E-7"/>
    <Resource URI="http://dbpedia.org/resource/Smoking_gun" support="28" types="" surfaceForm="smoking gun" offset="68" similarityScore="0.9942367821210603" percentageOfSecondRank="0.005796625092271743"/>
    <Resource URI="http://dbpedia.org/resource/Email" support="4760" types="" surfaceForm="email" offset="81" similarityScore="0.9998441228208889" percentageOfSecondRank="1.294724256058748E-4"/>
    <Resource URI="http://dbpedia.org/resource/Barack_Obama" support="14695" types="DBpedia:Agent,Schema:Person,Http://xmlns.com/foaf/0.1/Person,DBpedia:Person,DBpedia:OfficeHolder" surfaceForm="Obama" offset="103" similarityScore="0.99366844805149" percentageOfSecondRank="0.006369900896618705"/>
    <Resource URI="http://dbpedia.org/resource/United_States_Department_of_Justice" support="4536" types="DBpedia:Agent,Schema:Organization,DBpedia:Organisation,Schema:GovernmentOrganization,DBpedia:GovernmentAgency" surfaceForm="Justice Department" offset="109" similarityScore="0.9999993701077011" percentageOfSecondRank="5.355039299447172E-7"/>
    <Resource URI="http://dbpedia.org/resource/Settlement_(litigation)" support="727" types="" surfaceForm="settlement" offset="138" similarityScore="0.984034181518778" percentageOfSecondRank="0.010762752853438246"/>
    <Resource URI="http://dbpedia.org/resource/Liberalism" support="6101" types="" surfaceForm="liberal" offset="215" similarityScore="0.42136818389223374" percentageOfSecondRank="0.7737018249713564"/>
    <Resource URI="http://dbpedia.org/resource/United_States_Department_of_Justice" support="4536" types="DBpedia:Agent,Schema:Organization,DBpedia:Organisation,Schema:GovernmentOrganization,DBpedia:GovernmentAgency" surfaceForm="DOJ" offset="253" similarityScore="1.0" percentageOfSecondRank="0.0"/>
</Resources>
</Annotation>

我在PHP中的测试(不起作用):

$dom = new DOMDocument();
$dom->loadXML($documentXML);
$xpath = new DOMXPath($dom);

$bs = $documentXML
    ->getElementsByTagName('Annotation/Ressources/Ressource[URI]');

$arrayI = array();
for ($i = 0; $i<sizeof($arrayI); $i++) {
    foreach($bs as $URI){
        $arrayI[$i] = $URI->nodeValue . "\n";
    }
}

感谢您的帮助:)

1 个答案:

答案 0 :(得分:2)

DOMElement::getElementsByTagName()不支持Xpath,只支持标记名称。 DOMXpath::evaluate()是您需要的方法。

$dom = new DOMDocument();
$dom->loadXML($documentXML);
$xpath = new DOMXPath($dom);

$result = [];
foreach ($xpath->evaluate('Annotation/Resources/Resource') as $resourceNode) {
  $result[$resourceNode->getAttribute('surfaceForm')][] = 
    $resourceNode->getAttribute('URI');
}

var_dump($result);

您需要迭代Resource个元素节点,然后读取它们的属性并根据需要构建数组结构。