使用XML / Xpath / PHP获取元素名称/值

I have an XML schema that looks as follows:

<xml>
  <user id="1">
    <first_name>Bill</first_name>
    <last_name>Steve</last_name>
    <phone_numbers>
      <work>xxx-xxx-xxxx</work>
      <home>xxx-xxx-xxxx</home>
    </phone_numbers>
   </user>
   <user id="2">
      ........
   </user>
</xml>

Im working on parsing all of this information into PHP using DOM. Ex.

$userInfo = $doc->getElementsByTagName( "user" ); 
foreach($userInfo as $row)
{
       $first_name = $row->getElementsByTagName("first_name");
}

When I try to nest this to select the phone numbers however I get an error. I've tried using XPath to select the phone numbers with equal problems. I tried something along the lines of

$userInfo = $doc->getElementsByTagName( "user" ); 
foreach($userInfo as $row)
{
       $phoneInfo = $row->getElementsByTagName("phone_numbers");
       foreach($phoneInfo as $row2)
       {
            $work = $row2->getElementsByTagName("work");
       }
}

Im curious if Im doing something fundamentally wrong, or how to get this going. I've been tearing my hair out for a few hours now.

duanmu5039
duanmu5039 没有问题的XPath使用。重新标记
9 年多之前 回复
dongyan1491
dongyan1491 getElementsByTagName返回DOMNodeList。你确定你可以使用foreach迭代它吗?(只是问问而已)
9 年多之前 回复
dongyan7851
dongyan7851 适合我!你用哪种PHP?
9 年多之前 回复

2个回答

You can't get the value directly from a DOMNodeList Object, try this :

$userInfo = $doc->getElementsByTagName( "user" ); 
foreach($userInfo as $row)
{
       $phoneInfo = $row->getElementsByTagName("phone_numbers");
       foreach($phoneInfo as $row2)
       {
            // get the value from the first child
            $work = $row2->getElementsByTagName("work")->item(0)->nodeValue;
            $home = $row2->getElementsByTagName("home")->item(0)->nodeValue;
       }
}
duan5991518
duan5991518 我现在想吻你的嘴巴。 这就像一个魅力。 当它如此简单时,我喜欢/讨厌它。
9 年多之前 回复

Well, you could switch it to SimpleXml which makes this type of parsing easier:

$userInfo = $doc->getElementsByTagName( "user" ); 
foreach ($userInfo as $user) {
    $node = simplexml_import_dom($user);
    $id = (string) $node['id'];
    $first = (string) $node->first_name;
    $last = (string) $node->last_name;
    $workPhone = (string) $node->phone_numbers->work;
    $homePhone = (string) $node->phone_numbers->home;
}

Now, in DomDocument, you could do this by using DomXpath:

$userInfo = $doc->getElementsByTagName( "user" ); 
$xpath = new DomXpath($doc);
foreach ($userInfo as $user) {
    $id = $user->getAttribute('id');
    $first = $xpath->query('//first_name', $user)->item(0)->textContent;
    $last = $xpath->query('//last_name', $user)->item(0)->textContent;
    $work = $xpath->query('//phone_numbers/work', $user)->item(0)->textContent;
    $home = $xpath->query('//phone_numbers/home', $user)->item(0)->textContent;
}

Note that the above code (both parts) require that the format is exactly that. If you have conditionals, you might want to change it to something like this (the firstname conditional only):

$userInfo = $doc->getElementsByTagName( "user" ); 
$xpath = new DomXpath($doc);
foreach ($userInfo as $user) {
    $id = $user->getAttribute('id');
    $firstQuery = $xpath->query('//first_name', $user);
    if ($firstQuery->length > 0) {
        $first = $firstQuery->item(0)->textContent;
    } else {
        $first = '';
    }
}
duancuan7057
duancuan7057 我正在尝试使用你在这里提到的DomXpath,但我遇到的问题是区分哪个用户与电话号码相关联。 看起来像在$ userInfo返回的对象上使用xpath会起作用,我没有想到这一点。 另一个答案更接近我所需要的,但这看起来非常可靠。 感谢您抽出宝贵的时间来帮助我,当我再次深入xpath时,我确定我会使用它。
9 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐