将JSON转换为XML时限制XML数据 - PHP
问题描述:
我正在使用此代码将JSON转换为XML,并且一切正常。我在XML中获得200条记录,但我想将其限制为50.我只是想创建包含最新50条记录的XML文件。而不是将我们在JSON中的任何内容转换为XML。将JSON转换为XML时限制XML数据 - PHP
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>[email protected]</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>[email protected]</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>
答
很多这取决于你有多大的控制在数据。如果可以按时间戳排序数据,则可以在计数器达到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 ...” – orbnexus
您可以尝试$ count = $ count + 1; –
不..不起作用。我还尝试将数组限制为50,然后将它传递给aaray_to_xml函数,但没有对数据进行分片。 $ json_data = json_decode($ json_file,true); $ json_data = array_slice((array)$ json_data,0,50,true); print_r(array_count_values($ json_data)); – orbnexus