我创建一个数组,然后将0..n元素推到数组中的一个子数组上
(最容易理解我在下面代码中的意思)
当我通过XML序列化器运行数组时,它会在推送的元素周围添加额外的标记

$serializer_options = array(
    XML_SERIALIZER_OPTION_INDENT => '    ',
    XML_SERIALIZER_OPTION_RETURN_RESULT => TRUE,
    rootName => 'JZ_2103'
);
$serializer = &new XML_Serializer($serializer_options);

$otXmlArr = array(
    'status' => 'success',
    'reason' => 'null',
    'payload' => array(
        'ContactsResponse' => array()
    )
);

foreach ($mydata as $item) {
    array_push($otXmlArr['payload']['ContactsResponse'],
        array(
            'Contact' => array(
                'ID' => $item['id'],
                'FirstName' => $item['fname']
            )
        )
    );
}

$otXml = $serializer->serialize($otXmlArr);

创建的XML有额外的标记<XML_Serializer_Tag>。如何消除这些标记?
<JZ_2103>
    <status>success</status>
    <reason>null</reason>
    <payload>
        <ContactsResponse>
            <XML_Serializer_Tag>
                <Contact>
                    <ID>10</ID>
                    <FirstName>Roy</FirstName>
                </Contact>
            </XML_Serializer_Tag>
        </ContactsResponse>
    </payload>
</JZ_2103>

Absr的解决方案几乎可以工作-它产生:
从阵列的打印:
Array
(
    [status] => success
    [reason] => null
    [payload] => Array
        (
            [ContactsResponse] => Array
                (
                    [0] => Array
                        (
                            [Contact] => Array
                                (
                                    [ID] => 3
                                    [FirstName] => Joe
                                )

                        )

                    [1] => Array
                        (
                            [Contact] => Array
                                (
                                    [ID] => 4
                                    [FirstName] => Fred
                                )

                        )

                )

        )

)

序列化的XML:
<JZ_2103>
    <status>success</status>
    <reason>null</reason>
    <payload>
        <ContactsResponse>
            <Contact>
                <ID>3</ID>
                <FirstName>Joe</FirstName>
            </Contact>
        </ContactsResponse>
        <ContactsResponse>
            <Contact>
                <ID>4</ID>
                <FirstName>Fred</FirstName>
            </Contact>
        </ContactsResponse>
    </payload>
</JZ_2103>

根据daniel的建议,xml输出为:
额外嵌套级别:-(
<JZ_2103>
    <status>success</status>
    <reason>null</reason>
    <payload>
        <ContactsResponse>
            <Contact>
                <Contact>
                    <ID>3</ID>
                    <FirstName>Joe</FirstName>
                </Contact>
            </Contact>
            <Contact>
                <Contact>
                    <ID>4</ID>
                    <FirstName>Fred</FirstName>
                </Contact>
            </Contact>
        </ContactsResponse>
    </payload>
</JZ_2103>

我将放弃使用序列化器,而直接使用simplexml构建xml。这一切都是为了节省时间而开始的,显然,它没有!
感谢丹尼尔和阿布斯特的帮助,即使这没能让我到达终点。

最佳答案

'mode' => 'simplexml'添加到$serializer\u选项应:

$serializer_options = array(
    XML_SERIALIZER_OPTION_INDENT => '    ',
    XML_SERIALIZER_OPTION_RETURN_RESULT => TRUE,
    'rootName' => 'JZ_2103',
    'mode' => 'simplexml',
);

或者使用常数,
$serializer_options = array(
    XML_SERIALIZER_OPTION_INDENT => '    ',
    XML_SERIALIZER_OPTION_RETURN_RESULT => TRUE,
    XML_SERIALIZER_OPTION_ROOT_NAME => 'JZ_2103',
    XML_SERIALIZER_OPTION_MODE => XML_SERIALIZER_MODE_SIMPLEXML,
);

在515线上找到了它。
//使用XML序列化程序模式SimpleXML将父名称用作标记名
//如果转换索引数组
XML_SERIALIZER_OPTION_MODE => XML_SERIALIZER_MODE_DEFAULT,

编辑:更新为指向实际的XML序列化程序代码库。
代码更改如下(感谢@danielpsc,请参阅注释):
$otXmlArr = array(
    'status' => 'success',
    'reason' => 'null',
    'payload' => array(
        'ContactsResponse' => array(
            'Contact' => array()
        )
    )
);

foreach ($mydata as $item) {
    array_push($otXmlArr['payload']['ContactsResponse']['Contact'],
        array(
            'ID' => $item['id'],
            'FirstName' => $item['fname']
        )
    );
}

edit2:对代码进行了更改

关于php - pear XML_Serializer-消除多余的标签,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19033706/

10-09 05:50