获取元素节点等于string的xml结果

ok,xml文件如下所示,设置为变量$ otherdata </ p>

 &lt; result&gt; 
&lt; sighting&gt;
&lt; name&gt; Johhny&lt; / name&gt;
&lt; last&gt; smith&lt; / last&gt;
&lt; phone&gt; 5551234&lt; / phone&gt; \ n&lt; / sighting&gt;
&lt; / result&gt;
</ code> </ pre>

和php代码如下所示</ p>

   $ dom = new DOMDocument; 
$ dom - &gt; load($ otherdata);
$ xpath = new DomXpath($ dom);

$ query ='// result / sighting [name =“Johhny” ] /。';
$ entries = $ xpath-&gt; query($ query);

foreach($ entries as $ entry){
$ newlat = $ entry-&gt; textContent;

echo $ newlat
}
</ code> </ pre>

我遇到麻烦的地方是试图获取'last'和'phone'属性中的值并将其设置为等于变量 以后存储和回应...谢谢</ p>
</ div>

展开原文

原文

ok, xml file looks like this, it is set to a variable $otherdata

<result>
 <sighting>
  <name>Johhny</name>
  <last>smith</last>
  <phone>5551234</phone>
 </sighting>
 </result>

and php code looks like this

$dom = new DOMDocument;
$dom ->load($otherdata);
$xpath = new DomXpath($dom);

$query = '//result/sighting[name = "Johhny"]/.';
$entries = $xpath->query($query);

foreach ($entries as $entry) {
 $newlat = $entry->textContent;

 echo $newlat
 }

where I am running into trouble is trying to get the value in the 'last' and 'phone' attribute and set it equal to variable to store and echo later...thanks

dongwusang0314
dongwusang0314 如果有人对如何用一个可以帮助很多的变量替换“Johnny”有任何想法,也可以作为奖励
6 年多之前 回复

2个回答



您可以使用</ p>

  $ query ='// result / sighting [name  =“Johhny”]'; 
</ code> </ pre>

作为直接选择 sighting </ code>元素的路径。 然后你可以读出内容并用</ p>

  foreach($ entries as $ entry){
$ last = $ entry-&gt; getElementsByTagName('last')进行更改 - &gt; item(0) - &gt; textContent;
$ entry-&gt; getElementsByTagName('name') - &gt; textContent = $ newName;
}
</ code> </ pre>
</ DIV>

展开原文

原文

You could use

$query = '//result/sighting[name = "Johhny"]';

as the path as that way you directly select the sighting element(s). Then you can read out the contents and change it with

foreach ($entries as $entry) {
 $last = $entry->getElementsByTagName('last')->item(0)->textContent;
 $entry->getElementsByTagName('name')->textContent = $newName;
 }

dqr3883
dqr3883 这对我的需求非常有用,谢谢
6 年多之前 回复
du5910
du5910 我编辑了我的答案并更改了代码。
6 年多之前 回复
duanmu0834
duanmu0834 感谢快速回复,遗憾的是我需要获得多个元素...我没有在同一个代码中列出它们。
6 年多之前 回复



通过这种方式,您可以浏览所有瞄准元素,并在这些元素中获得所有子元素。 现在,您可以将所有数据存储在一个数组中并稍后显示。</ p>

  $ data = array(); 
$ xml = new DOMDocument();
$ xml - &gt; load($ otherdata);

$ nodes = $ xml-&gt; getElementsByTagName('sighting');
foreach($ nodes as $ node){
$ children = $ node-&gt; childNodes;

$ i = 0;
foreach($ children as $ child){
$ data [$ i] [] = $ child-&gt; nodeValue;
}
}
</ code> </ pre>

这样你就可以更新name元素并保存xml doc。</ p>

  $ xml = new DOMDocument(); 
$ xml- &gt; load($ file);

$ nodes = $ xml-&gt; getElementsByTagName('sighting');
foreach($ nodes-&gt; item as $ node){
$ children = $ node-&gt ;的childNodes;

foreach($ children as $ child){
if($ child-&gt; nodeName =='name')
$ child-&gt; nodeValue ='Not Johnny';
}
}

\ n $ xml-&gt; save($ file);
</ code> </ pre>
</ div>

展开原文

原文

This way you run through all sighting elements and within those elements you get all child elements. Now you can store all your data in an array and display it later.

$data = array();
$xml = new DOMDocument();
$xml->load($otherdata);

$nodes = $xml->getElementsByTagName('sighting');
foreach ($nodes as $node) {
    $children = $node->childNodes; 
    $i=0;
    foreach ($children as $child) { 
        $data[$i][] = $child->nodeValue;
    }
}

This way you can update the name element and save the xml doc.

$xml = new DOMDocument();
$xml->load($file);

$nodes = $xml->getElementsByTagName('sighting');
foreach ($nodes->item as $node) {
    $children = $node->childNodes; 
    foreach ($children as $child) { 
        if ($child->nodeName == 'name')
            $child->nodeValue = 'Not Johnny';
    } 
}

$xml->save($file);

立即提问