我在我的项目中使用CURL。当我发送请求时,响应将以XML的形式出现,如下所示。如何将此xml转换为数组以在网页中显示它?
此XML文件似乎没有与之关联的任何样式信息。
文档树如下所示。
<Response status="OK" responseTime="10">
<Results>
<wirelineServices>
<blockFIPS>360775908001027</blockFIPS>
<frn>0007556251</frn>
<providerName>Time Warner Cable LLC</providerName>
<doingBusinessAs>Time Warner Cable</doingBusinessAs>
<holdingCompanyNumber>131352</holdingCompanyNumber>
<holdingCompanyName>Time Warner Cable Inc.</holdingCompanyName>
<providerURL>www.timewarnercable.com</providerURL>
<technologies>
<technologyCode>40</technologyCode>
<typicalDownloadSpeed>0</typicalDownloadSpeed>
<typicalUploadSpeed>0</typicalUploadSpeed>
<maximumAdvertisedDownloadSpeed>9</maximumAdvertisedDownloadSpeed>
<maximumAdvertisedUploadSpeed>5</maximumAdvertisedUploadSpeed>
<overallQuality>1</overallQuality>
<speedQuality>1.00</speedQuality>
<technologyQuality>3.00</technologyQuality>
<providerQuality>3.00</providerQuality>
<downloadQuality>1.00</downloadQuality>
<uploadQuality>1.00</uploadQuality>
<maximumProviderScore>3.00</maximumProviderScore>
<maximumTechnologyScore>3.00</maximumTechnologyScore>
<maximumSpeedScore>2.00</maximumSpeedScore>
<maximumDownloadScore>2.00</maximumDownloadScore>
<maximumUploadScore>2.00</maximumUploadScore>
</technologies>
</wirelineServices>
<wirelineServices>
<blockFIPS>360775908001027</blockFIPS>
<frn>0003469442</frn>
<providerName>Verizon New York Inc.</providerName>
<doingBusinessAs>Verizon New York</doingBusinessAs>
<holdingCompanyNumber>131425</holdingCompanyNumber>
<holdingCompanyName>Verizon Communications Inc.</holdingCompanyName>
<providerURL>www.connecttoverizon.com</providerURL>
<technologies>
<technologyCode>10</technologyCode>
<typicalDownloadSpeed>0</typicalDownloadSpeed>
<typicalUploadSpeed>0</typicalUploadSpeed>
<maximumAdvertisedDownloadSpeed>5</maximumAdvertisedDownloadSpeed>
<maximumAdvertisedUploadSpeed>3</maximumAdvertisedUploadSpeed>
<overallQuality>1</overallQuality>
<speedQuality>1.00</speedQuality>
<technologyQuality>2.00</technologyQuality>
<providerQuality>2.00</providerQuality>
<downloadQuality>0.00</downloadQuality>
<uploadQuality>1.00</uploadQuality>
<maximumProviderScore>3.00</maximumProviderScore>
<maximumTechnologyScore>2.00</maximumTechnologyScore>
<maximumSpeedScore>2.00</maximumSpeedScore>
<maximumDownloadScore>2.00</maximumDownloadScore>
<maximumUploadScore>2.00</maximumUploadScore>
</technologies>
</wirelineServices>
<broadbandSource>
<stateFips>36</stateFips>
<organization>The New York State Office of Cyber Security</organization>
<organizationURL>http://www.cscic.state.ny.us/broadband/</organizationURL>
</broadbandSource>
</Results>
</Response>
PHP脚本
//Setup cURL Request
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $request_url);
curl_setopt($curl, CURLOPT_TIMEOUT, 130);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($curl);
curl_close($curl);
$xml = simplexml_load_string($response);
$json = json_encode($xml);
$arr = json_decode($json,true);
答案 0 :(得分:0)
你的答案会有效,但我很好奇你为什么要将json_decode创建的数组转换成另一个数组呢?您可以使用simplexml_load_string返回的xml object在页面上显示数据。您还可以显示json_decode的数组输出中的数据。您还可以使用XSLT来向您的XML添加css。
答案 1 :(得分:0)
也许这可以帮到你,在PHP.net找到它。发布者:efredricksen。
<?php
class XmlElement {
var $name;
var $attributes;
var $content;
var $children;
};
function xml_to_object($xml) {
$parser = xml_parser_create();
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
xml_parse_into_struct($parser, $xml, $tags);
xml_parser_free($parser);
$elements = array(); // the currently filling [child] XmlElement array
$stack = array();
foreach ($tags as $tag) {
$index = count($elements);
if ($tag['type'] == "complete" || $tag['type'] == "open") {
$elements[$index] = new XmlElement;
$elements[$index]->name = $tag['tag'];
$elements[$index]->attributes = $tag['attributes'];
$elements[$index]->content = $tag['value'];
if ($tag['type'] == "open") { // push
$elements[$index]->children = array();
$stack[count($stack)] = &$elements;
$elements = &$elements[$index]->children;
}
}
if ($tag['type'] == "close") { // pop
$elements = &$stack[count($stack) - 1];
unset($stack[count($stack) - 1]);
}
}
return $elements[0]; // the single top-level element
}
// For example:
$xml = '
<Response status="OK" responseTime="10">
<Results>
<wirelineServices>
<blockFIPS>360775908001027</blockFIPS>
<frn>0007556251</frn>
<providerName>Time Warner Cable LLC</providerName>
<doingBusinessAs>Time Warner Cable</doingBusinessAs>
<holdingCompanyNumber>131352</holdingCompanyNumber>
<holdingCompanyName>Time Warner Cable Inc.</holdingCompanyName>
<providerURL>www.timewarnercable.com</providerURL>
<technologies>
<technologyCode>40</technologyCode>
<typicalDownloadSpeed>0</typicalDownloadSpeed>
<typicalUploadSpeed>0</typicalUploadSpeed>
<maximumAdvertisedDownloadSpeed>9</maximumAdvertisedDownloadSpeed>
<maximumAdvertisedUploadSpeed>5</maximumAdvertisedUploadSpeed>
<overallQuality>1</overallQuality>
<speedQuality>1.00</speedQuality>
<technologyQuality>3.00</technologyQuality>
<providerQuality>3.00</providerQuality>
<downloadQuality>1.00</downloadQuality>
<uploadQuality>1.00</uploadQuality>
<maximumProviderScore>3.00</maximumProviderScore>
<maximumTechnologyScore>3.00</maximumTechnologyScore>
<maximumSpeedScore>2.00</maximumSpeedScore>
<maximumDownloadScore>2.00</maximumDownloadScore>
<maximumUploadScore>2.00</maximumUploadScore>
</technologies>
</wirelineServices>
<wirelineServices>
<blockFIPS>360775908001027</blockFIPS>
<frn>0003469442</frn>
<providerName>Verizon New York Inc.</providerName>
<doingBusinessAs>Verizon New York</doingBusinessAs>
<holdingCompanyNumber>131425</holdingCompanyNumber>
<holdingCompanyName>Verizon Communications Inc.</holdingCompanyName>
<providerURL>www.connecttoverizon.com</providerURL>
<technologies>
<technologyCode>10</technologyCode>
<typicalDownloadSpeed>0</typicalDownloadSpeed>
<typicalUploadSpeed>0</typicalUploadSpeed>
<maximumAdvertisedDownloadSpeed>5</maximumAdvertisedDownloadSpeed>
<maximumAdvertisedUploadSpeed>3</maximumAdvertisedUploadSpeed>
<overallQuality>1</overallQuality>
<speedQuality>1.00</speedQuality>
<technologyQuality>2.00</technologyQuality>
<providerQuality>2.00</providerQuality>
<downloadQuality>0.00</downloadQuality>
<uploadQuality>1.00</uploadQuality>
<maximumProviderScore>3.00</maximumProviderScore>
<maximumTechnologyScore>2.00</maximumTechnologyScore>
<maximumSpeedScore>2.00</maximumSpeedScore>
<maximumDownloadScore>2.00</maximumDownloadScore>
<maximumUploadScore>2.00</maximumUploadScore>
</technologies>
</wirelineServices>
<broadbandSource>
<stateFips>36</stateFips>
<organization>The New York State Office of Cyber Security</organization>
<organizationURL>http://www.cscic.state.ny.us/broadband/</organizationURL>
</broadbandSource>
</Results>
</Response>
';
echo "<pre>";
@print_r(xml_to_object($xml));
?>