【发布时间】:2017-10-10 04:58:48
【问题描述】:
试图找到一种方法来读取 XML 文件并获取所有节点。 一些 XML 文件的深度为 1,其他的为 2,其他的更多。 有没有办法在不知道他们名字的情况下为所有孩子获取所有节点? 例如我写了这段代码,但这仅适用于 2 步深度
foreach ($xml->children() as $node) {
if ($node->children()->count()>0) {
foreach ($node->children() as $cnode){
echo $cnode->getName()."<br>";
}
}
echo $node->getName()."<br>";
}
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" >
<soap:Header/>
<soap:Body>
<multitrans xmlns="http://www.xxxx.xx/">
<authentication>
<username>xxxxxx</username>
<password>xxxxxx</password>
<clientid>xxxxxx</clientid>
</authentication>
<requests>
<trans_request>
<TransType tc="100"/>
<company tc="500"/>
<product tc="auto"/>
<inception>
<p_year>2017</p_year>
<p_month>5</p_month>
<p_day>15</p_day>
</inception>
<p_number>0</p_number>
<attributes>
<att val="0" name="SVCsynchronouscall" />
<att val="2" name="value1" />
<att val="2017-5-15" name="Date" />
<att val="0" name="value2" />
<att val="0" name="value3" />
<att val="0" name="value4" />
</attributes>
<warnings>
</warnings>
</trans_request>
</requests>
</multitrans>
</soap:Body>
</soap:Envelope>
【问题讨论】:
-
请分享您的 xml 字符串。
-
@SahilGulati 我已添加到我的问题中
-
您要提取哪些信息?
-
我必须获取所有节点名称。例如用户名、密码、clientid、transtype、company、product、p_year 等以及所有属性名称(例如 SVCsynchronouscall、value1 等)
-
希望我的帖子能帮到你..