使用SimpleXML不返回的某些元素属性加载XML [英] Loading XML using SimpleXML doesn't return attributes on some elements
问题描述
我已加载使用XML文件
使用simplexml_load_file($文件路径的SimpleXMLElement',LIBXML_NOCDATA);
对于一般的XML提供它工作正常。然而,对于一些在XML元素的属性不被转换成一个'@属性阵列,并且代替缺少形成输出。下面是一个示例:
< UI_DEFINITION>
< EDIT_PERMISSION> testPermission< / EDIT_PERMISSION>
< DEFAULT_VALUES>
<显示>隐藏< /显示>
< css_class> generic_css_class< / css_class>
<标题> {标签}< /标题>
<&型GT;串LT; /类型>
< wrapper_format> {值}< / wrapper_format>
< full_path>假LT; / full_path>
<&强制性GT假LT; /强制性>
< edit_permission> testPermission< / edit_permission>
&所述; MAX_LENGTH大于0&下; / MAX_LENGTH>
< / DEFAULT_VALUES>
<&查找GT;
< DB_LOOKUP NAME =TEST3>
<视图> ???< /视图>
< ID_FIELD> ???< / ID_FIELD>
< DESCR_FIELD> ???< / DESCR_FIELD>
<排序> ??? ASC< /排序>
< / DB_LOOKUP>
< DB_LOOKUP NAME =测试1>
<视图> ???< /视图>
< ID_FIELD> ???< / ID_FIELD>
< DESCR_FIELD> ???< / DESCR_FIELD>
<排序> ??? ASC< /排序>
< / DB_LOOKUP>
< /查找>
<面积internal_name =main_details称号=显示=秀>
<字段查找=测试1标题=标题>标题< / FIELD>
<字段标题=名称> GIVEN_NAME< / FIELD>
<字段title =中间名(S)> Middle_Names< / FIELD>
<字段标题=姓>&FAMILY_NAME LT; / FIELD>
<字段标题=性别>性别与LT; / FIELD>
<字段标题=诞生TYPE =日期>&DATE_OF_BIRTH LT; / FIELD>
<现场MAX_LENGTH =20标题=ID> Unique_Identifier< / FIELD>
< / AREA>
这给出了print_r的输出如下(我已经添加在这就是问题所在位换行):
SimpleXMLElement对象([UI_DEFINITION] => SimpleXMLElement对象( => testPermission [DEFAULT_VALUES] => SimpleXMLElement对象([显示] =>隐藏[css_class] => generic_css_class [标题] => {标签}【类型】=>字符串[wrapper_format] => {值} [full_path] =>假[强制性] =>假[edit_permission] => testPermission [MAX_LENGTH] = 0)[查询] => SimpleXMLElement对象([DB_LOOKUP] =>数组([0] => SimpleXMLElement对象([@属性] =>阵列([名称] => TEST3 )[查看] => ??? [ID_FIELD] => ??? [DESCR_FIELD] => ??? [订购] => ??? ASC)[1] => SimpleXMLElement对象([@属性] =>阵列([名称] =>测试1)[查看] => ??? [ID_FIELD] => ??? [DESCR_FIELD] => ??? [订购] => ?? ?ASC)))
[区域] => SimpleXMLElement对象([@属性] =>数组([internal_name] => main_details [标题] => [显示] =>显示)[字段] =>数组([0] =>标题[1 ] => GIVEN_NAME [2] => Middle_Names [3] => FAMILY_NAME [4] =>性别[5] => DATE_OF_BIRTH [6] => Unique_Identifier))))
正如你所看到的,属性阵列正确添加到大多数元素,但不能到外地元素。我已经试过重新命名他们,这似乎并没有发挥作用。
编辑:
我还要补充一点,我已经试过了FIELDS标签周围的字段标识,也无济于事。
编辑:
我已经简化了XML巨大,它仍然没有返回ANNY属性:
< UI_DEFINITION>
<字段查找=测试1标题=标题>标题< / FIELD>
< / UI_DEFINITION>
生产:
SimpleXMLElement对象([UI_DEFINITION] => SimpleXMLElement对象([字段] =>标题))
对不起它采取这么久才回来,回答这个问题!
正如刚才code提示,属性是访问。我在的SimpleXML的序列化问题对象为另一种格式。使用printr或json_convert的,而对象上导致了不是在报告的病例中可用的属性。
我没有远远不够进入这个找到打印的code为基础的解决办法,或将这些对象包括有问题的情况下,我只是周围工作作为XML数据的一部分:
< UI_DEFINITION>
<字段查找=测试1标题=标题>< VALUEPATH>标题< / VALUEPATH>< / FIELD>
< / UI_DEFINITION>
Addint这个额外级别成层次结构导致的属性是在顶层pserved $ P $,和文本值在子级是可用的正确
I have loaded an XML file using
simplexml_load_file($filePath,'SimpleXMLElement', LIBXML_NOCDATA);
And for most of the XML provided it works fine. However, for some of the elements in the XML the attributes are not converted into an '@attributes' array, and are instead missing form the output. Here's a sample:
<UI_DEFINITION>
<EDIT_PERMISSION>testPermission</EDIT_PERMISSION>
<DEFAULT_VALUES>
<display>hidden</display>
<css_class>generic_css_class</css_class>
<title>{tag}</title>
<type>string</type>
<wrapper_format>{value}</wrapper_format>
<full_path>false</full_path>
<mandatory>false</mandatory>
<edit_permission>testPermission</edit_permission>
<max_length>0</max_length>
</DEFAULT_VALUES>
<LOOKUPS>
<DB_LOOKUP name="test3">
<VIEW>???</VIEW>
<ID_FIELD>???</ID_FIELD>
<DESCR_FIELD>???</DESCR_FIELD>
<ORDER>??? asc</ORDER>
</DB_LOOKUP>
<DB_LOOKUP name="test1">
<VIEW>???</VIEW>
<ID_FIELD>???</ID_FIELD>
<DESCR_FIELD>???</DESCR_FIELD>
<ORDER>??? asc</ORDER>
</DB_LOOKUP>
</LOOKUPS>
<AREA internal_name="main_details" title="" display="show">
<FIELD lookup="test1" title="Title">Title</FIELD>
<FIELD title="Name">Given_Name</FIELD>
<FIELD title="Mid. Name(s)">Middle_Names</FIELD>
<FIELD title="Family Name">Family_Name</FIELD>
<FIELD title="Gender">Gender</FIELD>
<FIELD title="Born" type="date">Date_of_Birth</FIELD>
<FIELD max_length="20" title="ID">Unique_Identifier</FIELD>
</AREA>
This gives the following output from print_r (I've added a line break at the bit that's the problem):
SimpleXMLElement Object ( [UI_DEFINITION] => SimpleXMLElement Object ( [EDIT_PERMISSION] => testPermission [DEFAULT_VALUES] => SimpleXMLElement Object ( [display] => hidden [css_class] => generic_css_class [title] => {tag} [type] => string [wrapper_format] => {value} [full_path] => false [mandatory] => false [edit_permission] => testPermission [max_length] => 0 ) [LOOKUPS] => SimpleXMLElement Object ( [DB_LOOKUP] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [name] => test3 ) [VIEW] => ??? [ID_FIELD] => ??? [DESCR_FIELD] => ??? [ORDER] => ??? asc ) [1] => SimpleXMLElement Object ( [@attributes] => Array ( [name] => test1 ) [VIEW] => ??? [ID_FIELD] => ??? [DESCR_FIELD] => ??? [ORDER] => ??? asc ) ) )
[AREA] => SimpleXMLElement Object ( [@attributes] => Array ( [internal_name] => main_details [title] => [display] => show ) [FIELD] => Array ( [0] => Title [1] => Given_Name [2] => Middle_Names [3] => Family_Name [4] => Gender [5] => Date_of_Birth [6] => Unique_Identifier ) ) ) )
As you can see, the attributes array is correctly added to most of the elements, but not to the FIELD elements. I've tried renaming them and it didn't seem to make a difference.
EDIT: I should also add that I've tried surrounding the FIELD tags with a FIELDS tag, also to no avail.
EDIT:
I've simplified the XML hugely, and it still doesn't return anny attributes:
<UI_DEFINITION>
<FIELD lookup="test1" title="Title">Title</FIELD>
</UI_DEFINITION>
produces:
SimpleXMLElement Object ( [UI_DEFINITION] => SimpleXMLElement Object ( [FIELD] => Title ) )
Sorry it's taken so long to come back and answer this question!
As MrCode suggested, the attributes were accessible. The problem I was in the serialisation of the SimpleXML object into another format. Using printr or json_convert on the while object resulted in the attributes not being available in the cases reported.
I didn't go far enough into this to find a code-based workaround for printing or converting these objects including the problematic cases, I simply worked around it as part of the XML data:
<UI_DEFINITION>
<FIELD lookup="test1" title="Title"><VALUEPATH>Title</VALUEPATH></FIELD>
</UI_DEFINITION>
Addint this extra level into the hierarchy resulted in the attributes being preserved at the top level, and the text value being available correctly at the sub-level.
这篇关于使用SimpleXML不返回的某些元素属性加载XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!