【问题标题】:Fetching node name using XPath使用 XPath 获取节点名称
【发布时间】:2012-11-22 12:35:18
【问题描述】:

我有这个 XML 并希望在节点“ObjectDetails”中获取第二个元素的名称。 名字叫“库普”

$xml = '
    <ObjectenLijst TimeStamp="17-07-2012 09:14:46" Versie="12">
        <Object>
            <ObjectDetails>
                <Adres>Niebergweg 1</Adres>
                <Koop>
                    <Prijsvoorvoegsel>vraagprijs</Prijsvoorvoegsel>
                    <Koopprijs>31000</Koopprijs>
                    <KoopConditie>kosten koper</KoopConditie>
                </Koop>
            </ObjectDetails>
        </Object>
    <ObjectenLijst>' ;

当我使用这个 XPath 时,返回一个空数组:

$xml = simplexml_load_string($xml);
$result =  $xml->xpath('name(//Object/ObjectDetails/*[2])');

【问题讨论】:

    标签: php xml xpath simplexml


    【解决方案1】:

    据我所知,SimpleXMLElement::xpath 只能返回 SimpleXMLElement 节点的数组,因此您尝试执行的操作将无效。但是,您可以通过这种方式获取节点的名称:

    $result =  $xml->xpath('/ObjectenLijst/Object/ObjectDetails/*[2]');
    $name = $result[0]->getName(); // Koop
    

    【讨论】:

      猜你喜欢
      • 2021-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-19
      • 1970-01-01
      • 1970-01-01
      • 2012-10-09
      相关资源
      最近更新 更多