Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从PHP中的DomeElement获取特定的子标记_Php_Xml_Dom - Fatal编程技术网

从PHP中的DomeElement获取特定的子标记

从PHP中的DomeElement获取特定的子标记,php,xml,dom,Php,Xml,Dom,我正在浏览一个xml定义文件,我有一个域名列表。 我需要提取当前实体中可能存在或不存在的子标记的内容 <input id="name"> <label>Full Name:</label> <required /> </input> <input id="phone"> <required /> </input> <input id="email" /> 预期结果: Arra

我正在浏览一个xml定义文件,我有一个域名列表。 我需要提取当前实体中可能存在或不存在的子标记的内容

<input id="name">
  <label>Full Name:</label>
  <required />
</input>
<input id="phone">
  <required />
</input>
<input id="email" />
预期结果:

Array(
  ['name'] => "Full Name:"
  ['phone'] => 
)

奇怪的是:你已经知道答案了,因为你已经在剧本中使用了它。
但这次不是将DOMDocument作为上下文“节点”,而是使用
input
domeElement:

<?php
$doc = getDoc();
foreach( $doc->getElementsByTagName('required') as $e ) {
  $e = $e->parentNode; // this should be the <input> element
  // all <label> elements that are direct children of this <input> element
  foreach( $e->getElementsByTagName('label') as $l ) {
    echo 'label="', $l->nodeValue, "\"\n";
  }
}

function getDoc() {
  $doc = new DOMDocument;
  $doc->loadxml('<foo>
    <input id="name">
      <label>Full Name:</label>
      <required />
    </input>
    <input id="phone">
      <required />
    </input>
    <input id="email" />
  </foo>');
  return $doc;
}

Rock,可以了,谢谢!
<?php
$doc = getDoc();
foreach( $doc->getElementsByTagName('required') as $e ) {
  $e = $e->parentNode; // this should be the <input> element
  // all <label> elements that are direct children of this <input> element
  foreach( $e->getElementsByTagName('label') as $l ) {
    echo 'label="', $l->nodeValue, "\"\n";
  }
}

function getDoc() {
  $doc = new DOMDocument;
  $doc->loadxml('<foo>
    <input id="name">
      <label>Full Name:</label>
      <required />
    </input>
    <input id="phone">
      <required />
    </input>
    <input id="email" />
  </foo>');
  return $doc;
}