$ Xml-> children()不使用:commons标记

时间:2014-10-14 17:40:56

标签: php xml xml-parsing

   <listing> 
<Community>
            <commons:Subdivision commons:isgSecurityClass="Public">Cheat Crossings</commons:Subdivision>
            <commons:Schools>
                <commons:School>
                    <commons:Name>Valley View</commons:Name>
                    <commons:SchoolCategory>Elementary</commons:SchoolCategory>
                    <commons:District commons:isgSecurityClass="Public">Monongalia</commons:District>
                    <commons:Description>true</commons:Description>
                </commons:School>
                <commons:School>
                    <commons:Name>MHS</commons:Name>
                    <commons:SchoolCategory>High</commons:SchoolCategory>
                    <commons:District commons:isgSecurityClass="Public">Monongalia</commons:District>
                    <commons:Description>true</commons:Description>
                </commons:School>
                <commons:School>
                    <commons:Name>Morgantown Jr High School</commons:Name>
                    <commons:SchoolCategory>JuniorHigh</commons:SchoolCategory>
                    <commons:District commons:isgSecurityClass="Public">Monongalia</commons:District>
                    <commons:Description>true</commons:Description>
                </commons:School>
                <commons:School>
                    <commons:Name>South</commons:Name>
                    <commons:SchoolCategory>Middle</commons:SchoolCategory>
                    <commons:District commons:isgSecurityClass="Public">Monongalia</commons:District>
                    <commons:Description>true</commons:Description>
                </commons:School>
            </commons:Schools>
        </Community>

我如何获得学校标记下的所有学校记录? 我试过了,但它没有用。 $ eachListing-&GT;以社区&GT; Schools-&GT;儿童(&#34;公共&#34;,TRUE);

1 个答案:

答案 0 :(得分:0)

这里应该是某个地方的xmlns:commons =“...”属性。该属性的值是实际的命名空间,'common'前缀只是它的别名。使用命名空间在应用程序中定义变量或常量并使用它,而不是别名/前缀。

const XMLNS_COMMON = 'http://rets.org/xsd/RETSCommons'; // the value from the xmlns:common attribute?
$eachListing->Community->Schools->children(XMLNS_COMMON);

可以在文档中的每个节点上更改别名/前缀,这不是必需的。不要以你的逻辑为基础。

对于不在文档元素名称空间中的任何节点,可能需要执行此操作。

$nodes = $eachListing
  ->Community
  ->children(XMLNS_COMMON)->Schools
  ->children(XMLNS_COMMON);

演示:https://eval.in/205950