I am walking through a xml definition file and I have a DOMNodeList that I am walking through. I need to extract the contents of a child tag that may or may not be in the current entity
我正在浏览一个xml定义文件,我有一个DOMNodeList,我正在浏览。我需要提取子标记的内容,这个子标记可能在当前实体中,也可能不在当前实体中
<input id="name">
<label>Full Name:</label>
<required />
</input>
<input id="phone">
<required />
</input>
<input id="email" />
I need to replace ????????????? with something that gets me the contents of the label tag if it exists.
我需要更换???????????如果标签的内容存在的话,我就可以得到标签的内容。
Code:
代码:
foreach($dom->getElementsByTagName('required') as $required){
$curr = $required->parentNode;
$label[$curr->getAttribute('id')] = ?????????????
}
Expected Result:
预期结果:
Array(
['name'] => "Full Name:"
['phone'] =>
)
1 个解决方案
#1
8
Strange thing is: you already know the answer since you've used it in your script, getElementsByTagName().
But this time not with the DOMDocument as context "node" but with the input
DOMElement:
奇怪的是:您已经知道答案了,因为您已经在脚本getElementsByTagName()中使用了它。但这一次不使用DOMDocument作为上下文“节点”,而是使用输入DOMElement:
<?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;
}
prints label="Full Name:"
打印标签= "姓名:"
#1
8
Strange thing is: you already know the answer since you've used it in your script, getElementsByTagName().
But this time not with the DOMDocument as context "node" but with the input
DOMElement:
奇怪的是:您已经知道答案了,因为您已经在脚本getElementsByTagName()中使用了它。但这一次不使用DOMDocument作为上下文“节点”,而是使用输入DOMElement:
<?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;
}
prints label="Full Name:"
打印标签= "姓名:"