【问题标题】:$Xml->children() is not working with :commons tag$Xml->children() 不适用于 :commons 标签
【发布时间】:2014-10-14 17:40:55
【问题描述】:
   <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->社区->学校->儿童("commons",true);

【问题讨论】:

  • 你用什么来解析 xml(simpleXML 或 DOMDocument)?
  • 我正在使用 simpleXML
  • 你能显示commons命名空间的定义吗?
  • 不,我没有。我所有的那个xml和类型是“xs:string”

标签: php xml xml-parsing


【解决方案1】:

这里应该有一个 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

【讨论】:

  • 很抱歉,这对我不起作用。我使用了 simplexml_load_file 方法,错误是“节点不再存在”,当我使用 simplexml_load_string 时,错误是 Entity: line 1: parser error: Start tag expected, '
  • simplexml_load_string() 从 xml 字符串加载,simplexml_load_file() 从文件加载。您是否注意到该演示对命名空间中的每个节点都使用了 children() 方法?
  • 是的,我注意到了。但是每当我尝试时,它都会向我抛出这个错误“节点不再存在”。我的代码是 $nodes = $eachListing ->Community ->children(XMLNS_COMMON)->Schools ->children(XMLNS_COMMON); var_dump($nodes);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-15
  • 2014-08-17
  • 1970-01-01
  • 1970-01-01
  • 2020-05-25
  • 2019-01-30
相关资源
最近更新 更多