【问题标题】:Limit XML data when converting JSON to XML - PHP将 JSON 转换为 XML 时限制 XML 数据 - PHP
【发布时间】:2017-11-04 04:33:00
【问题描述】:

我正在使用此代码将 JSON 转换为 XML,并且一切正常。我在 XML 中获得 200 条记录,但我想将其限制为 50 条。我只想创建包含最新 50 条记录的 XML 文件。而不是将我们在 JSON 中的任何内容转换为 XML。

function array_to_xml( $data, &$xml_data ) {
    foreach( $data as $key => $value ) {
        if( is_numeric($key) ){
            $key = 'item'.$key; //dealing with <0/>..<n/> issues
        }
        if( is_array($value) ) {
            $subnode = $xml_data->addChild($key);
            array_to_xml($value, $subnode);
        } else {
            $xml_data->addChild("$key",htmlspecialchars("$value"));
        }
     }
}
$xml_data = new SimpleXMLElement('<?xml version="1.0"?><data></data>');
$json_file = file_get_contents("/directory/jsonfile.json");
$json_data = json_decode($json_file, true);
$json_data = array_slice((array)$json_data, 0, 50);
array_to_xml($json_data,$xml_data);
$result = $xml_data->asXML('/directory/xmlfile.xml');

XML

这是我的日期节点的样子:

<dateLastModified>1493913962397</dateLastModified>

XML 示例数据

<data>
    <total>212</total>
    <start>0</start>
    <count>212</count>
    <data>
        <item0>
            <id>123</id>
            <title>abc-test1</title>
            <clientContact>
                <id>111</id>
                <firstName>abc</firstName>
                <lastName>xyz</lastName>
                <email>abc@xyz.ca</email>
            </clientContact>
            <isOpen>1</isOpen>
            <isPublic>1</isPublic>
            <isJobcastPublished>1</isJobcastPublished>
            <owner>
                <id>222</id>
                <firstName>testname</firstName>
                <lastName>testlastname</lastName>
                <address>
                    <address1>test address,</address1>
                    <address2>test</address2>
                    <city>City</city>
                    <state>state</state>
                    <zip>2222</zip>
                    <countryID>22</countryID>
                    <countryName>Country</countryName>
                    <countryCode>ABC</countryCode>
                </address>
                <email>test@test.com</email>
                <customText1>test123</customText1>
                <customText2>testxyz</customText2>
            </owner>
            <publicDescription>
                <p>test info</p>
            </publicDescription>
            <status>test</status>
            <dateLastModified>22222</dateLastModified>
            <customText4>test1</customText4>
            <customText10>test123</customText10>
            <customText11>test</customText11>
            <customText16>rtest</customText16>
            <_score>123</_score>
        </item0>
        <item1>
        ...
        </item1>
        ...
    </data>
</data>

【问题讨论】:

    标签: php json xml rss


    【解决方案1】:

    很大程度上取决于您对数据的控制程度。如果您可以按时间戳对数据进行排序,那么您可以添加一个计数器并在计数器达到 50 时退出循环。

    function array_to_xml( $data, &$xml_data ) {
        $count = 0;
    foreach( $data as $key => $value ) {
        $count++;
        if($count == 50) {
           exit; //or return;
        }
        if( is_numeric($key) ){
            $key = 'item'.$key; //dealing with <0/>..<n/> issues
        }
        if( is_array($value) ) {
            $subnode = $xml_data->addChild($key);
            array_to_xml($value, $subnode);
        } else {
            $xml_data->addChild("$key",htmlspecialchars("$value"));
        }
     }
    }
    $xml_data = new SimpleXMLElement('<?xml version="1.0"?><data></data>');
    $json_file = file_get_contents("/directory/jsonfile.json");
    $json_data = json_decode($json_file, true);
    array_to_xml($json_data,$xml_data);
    $result = $xml_data->asXML('/directory/xmlfile.xml');
    

    【讨论】:

    • 感谢您的快速回复。我应用了您的解决方案,但没有奏效。当我回显 $count 时,它会呈现此“1234112312344567123412345678567891011...”
    • 你可以试试 $count = $count + 1;
    • 不.. 不起作用。在将数组传递给 aaray_to_xml 函数之前,我还尝试将数组限制为 50,但这并没有对数据进行切片。 $json_data = json_decode($json_file, true); $json_data = array_slice((array)$json_data, 0, 50, true); print_r (array_count_values($json_data));
    • 如果您可以发布数据样本,将会有所帮助。您可能需要在 $count = $count + 1 周围添加一个 IF 语句,其中 if(key == first_key) 然后递增计数器
    • 我在帖子中添加了一个示例 XML 数据
    猜你喜欢
    • 1970-01-01
    • 2012-02-08
    • 1970-01-01
    • 1970-01-01
    • 2012-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多