WEB开发网
开发学院WEB开发PHP XML 转成 数组对象 阅读

XML 转成 数组对象

 2012-05-17 07:26:29 来源:WEB开发网   
核心提示:将XML内容解析后返回一个对应的数组对象,并且可以通过参数设置来设置返回类型【数组、JSON】 默认:数组 由于是基于simplexml_load_string 对带有命名空间的XML解析不是很好,XML 转成 数组对象,会丢失命名空间内容 以上是在测试中得到的结论,后续版本会解决这个问题

将XML内容解析后返回一个对应的数组对象,并且可以通过参数设置来设置返回类型【数组、JSON】 默认:数组
由于是基于simplexml_load_string 对带有命名空间的XML解析不是很好,会丢失命名空间内容 以上是在测试中得到的结论,后续版本会解决这个问题。
这个可以满足一般的使用需求

DEMO 以解析struts2配置文件为例

function toJSON()
{
    require_once '../classes/XmlToArray.php';
    $XML=<<<XML
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- struts2委托spring管理 -->
    <constant name="struts.objectFactory" value="spring"/>
    <!-- /crm/emp/add.action -->
    <package name="crm_employee" extends="struts-default" namespace="/emp">
        <action name="add" class="addBean" method="add">
            <result>add.action</result>
            <result>/emp/add_suc.jsp</result>
        </action>
        <action name="list" class="listBean" method="list">
            <result>/emp/list.jsp</result>
        </action>
        <action name="delete" class="deleteBean" method="delete">
            <result>delete.action</result>
            <result>/emp/delete_suc.jsp</result>
        </action>
        <action name="update" class="updateBean" method="update">
            <result>update.action</result>
            <result>/emp/edit_suc.jsp</result>
        </action>
        <action name="edit" class="editBean" method="edit">
            <result>/emp/edit.jsp</result>
        </action>
        <!-- Add actions here -->
    </package>
</struts>

XML;

    header("Content-type: text/html; charset=utf-8") ;
    $xml_to_array = new XmlToArray();
    $xml_to_array->setXml($XML);
    // 当标签名与内置属性有冲突的时候可以自定义相关属性名,一般其概况不需要设置
    //$xml_to_array->setAttributeAsName("attributeAsName")->setContentAsName("contentasName");
    $r = $xml_to_array->parseXml(true);
     print_r( $r ) ;
}
// 打印结果:

{"struts":{"attributes":[],"content":"","constant":{"attributes":{"name":"struts.objectFactory","value":"spring"},"content":""},"package":{"attributes":{"name":"crm_employee","extends":"struts-default","namespace":"\/emp"},"content":"","action":[{"attributes":{"name":"add","class":"addBean","method":"add"},"content":"","result":[{"attributes":[],"content":"add.action"},{"attributes":[],"content":"\/emp\/add_suc.jsp"}]},{"attributes":{"name":"list","class":"listBean","method":"list"},"content":"","result":{"attributes":[],"content":"\/emp\/list.jsp"}},{"attributes":{"name":"delete","class":"deleteBean","method":"delete"},"content":"","result":{"attributes":[],"content":"\/emp\/delete_suc.jsp"}},{"attributes":{"name":"update","class":"updateBean","method":"update"},"content":"","result":{"attributes":[],"content":"\/emp\/edit_suc.jsp"}},{"attributes":{"name":"edit","class":"editBean","method":"edit"},"content":"","result":{"attributes":[],"content":"\/emp\/edit.jsp"}}]}}}
// 打印数组
function toArray()
{
    require_once '../classes/XmlToArray.php';
    $XML=<<<XML
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- struts2委托spring管理 -->
    <constant name="struts.objectFactory" value="spring"/>
    <!-- /crm/emp/add.action -->
    <package name="crm_employee" extends="struts-default" namespace="/emp">
        <action name="add" class="addBean" method="add">
            <result>add.action</result>
            <result>/emp/add_suc.jsp</result>
        </action>
        <action name="list" class="listBean" method="list">
            <result>/emp/list.jsp</result>
        </action>
        <action name="delete" class="deleteBean" method="delete">
            <result>delete.action</result>
            <result>/emp/delete_suc.jsp</result>
        </action>
        <action name="update" class="updateBean" method="update">
            <result>update.action</result>
            <result>/emp/edit_suc.jsp</result>
        </action>
        <action name="edit" class="editBean" method="edit">
            <result>/emp/edit.jsp</result>
        </action>
        <!-- Add actions here -->
    </package>
</struts>

XML;

    header("Content-type: text/html; charset=utf-8") ;
    $xml_to_array = new XmlToArray();
    $xml_to_array->setXml($XML);
    // 当标签名与内置属性有冲突的时候可以自定义相关属性名,一般其概况不需要设置
    //$xml_to_array->setAttributeAsName("attributeAsName")->setContentAsName("contentasName");
    $r = $xml_to_array->parseXml();
     print_r( $r ) ;
}
// 打印结果

Array
(
    [struts] => Array
        (
            [attributes] => Array
                (
                )

            [content] => 
            [constant] => Array
                (
                    [attributes] => Array
                        (
                            [name] => struts.objectFactory
                            [value] => spring
                        )

                    [content] => 
                )

            [package] => Array
                (
                    [attributes] => Array
                        (
                            [name] => crm_employee
                            [extends] => struts-default
                            [namespace] => /emp
                        )

                    [content] => 
                    [action] => Array
                        (
                            [0] => Array
                                (
                                    [attributes] => Array
                                        (
                                            [name] => add
                                            [class] => addBean
                                            [method] => add
                                        )

                                    [content] => 
                                    [result] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [attributes] => Array
                                                        (
                                                        )

                                                    [content] => add.action
                                                )

                                            [1] => Array
                                                (
                                                    [attributes] => Array
                                                        (
                                                        )

                                                    [content] => /emp/add_suc.jsp
                                                )

                                        )

                                )

                            [1] => Array
                                (
                                    [attributes] => Array
                                        (
                                            [name] => list
                                            [class] => listBean
                                            [method] => list
                                        )

                                    [content] => 
                                    [result] => Array
                                        (
                                            [attributes] => Array
                                                (
                                                )

                                            [content] => /emp/list.jsp
                                        )

                                )

                            [2] => Array
                                (
                                    [attributes] => Array
                                        (
                                            [name] => delete
                                            [class] => deleteBean
                                            [method] => delete
                                        )

                                    [content] => 
                                    [result] => Array
                                        (
                                            [attributes] => Array
                                                (
                                                )

                                            [content] => /emp/delete_suc.jsp
                                        )

                                )

                            [3] => Array
                                (
                                    [attributes] => Array
                                        (
                                            [name] => update
                                            [class] => updateBean
                                            [method] => update
                                        )

                                    [content] => 
                                    [result] => Array
                                        (
                                            [attributes] => Array
                                                (
                                                )

                                            [content] => /emp/edit_suc.jsp
                                        )

                                )

                            [4] => Array
                                (
                                    [attributes] => Array
                                        (
                                            [name] => edit
                                            [class] => editBean
                                            [method] => edit
                                        )

                                    [content] => 
                                    [result] => Array
                                        (
                                            [attributes] => Array
                                                (
                                                )

                                            [content] => /emp/edit.jsp
                                        )

                                )

                        )

                )

        )

)

1 2  下一页

Tags:XML 数组 对象

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接