Read XML dynamic PHP

空扰寡人 提交于 2019-12-11 08:45:41

问题


in my site in PHP I want to read an xml file like this:

<?xml version="1.0" standalone="yes"?>
<RETURNDATA lang="it-IT" type="COR" xsi:noNamespaceSchemaLocation="http://xmlv5test.travco.co.uk/trlink/schema/CountryRequestV6Rcv.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <MESSAGE>All Countries details and relevant city details</MESSAGE>
    <DATA COUNTRY_CODE="ABW" CURRENCY_CODE="EUR">
        <COUNTRY_NAME>Aruba</COUNTRY_NAME>
        <CURRENCY_NAME>euro</CURRENCY_NAME>
        <COUNTRY_CITIES>
            <CITY_DATA CITY_CODE="AUA">
                <CITY_NAME>Aruba</CITY_NAME>
            </CITY_DATA>
        </COUNTRY_CITIES>
    </DATA>
    <DATA COUNTRY_CODE="ALB" CURRENCY_CODE="EUR">
        <COUNTRY_NAME>Albania</COUNTRY_NAME>
        <CURRENCY_NAME>euro</CURRENCY_NAME>
        <COUNTRY_CITIES>
            <CITY_DATA CITY_CODE="TIA">
                <CITY_NAME>Tirana</CITY_NAME>
            </CITY_DATA>
        </COUNTRY_CITIES>
    </DATA>
    <DATA COUNTRY_CODE="ARE" CURRENCY_CODE="EUR">
        <COUNTRY_NAME>Emirati Arabi Uniti</COUNTRY_NAME>
        <CURRENCY_NAME>euro</CURRENCY_NAME>
        <COUNTRY_CITIES>
            <CITY_DATA CITY_CODE="DXB">
                <CITY_NAME>Dubai</CITY_NAME>
            </CITY_DATA>
            <CITY_DATA CITY_CODE="AAI">
                <CITY_NAME>Al Ain</CITY_NAME>
            </CITY_DATA>
            <CITY_DATA CITY_CODE="FJR">
                <CITY_NAME>Fujaira</CITY_NAME>
            </CITY_DATA>
            <CITY_DATA CITY_CODE="SSH">
                <CITY_NAME>Sharja</CITY_NAME>
            </CITY_DATA>
            <CITY_DATA CITY_CODE="RKT">
                <CITY_NAME>Ras al-Khaimah</CITY_NAME>
            </CITY_DATA>
            <CITY_DATA CITY_CODE="AUH">
                <CITY_NAME>Abu Dhabi</CITY_NAME>
            </CITY_DATA>
         </COUNTRY_CITIES>
     </DATA>
</RETURNDATA>

I want to enter in each node name DATA and take:
COUNTRY_CODE
CURRENCY_CODE
COUNTRY_NAME
CURRENCY_NAME
And all ountry cities code and name into an array associative.

I have tried with SimpleXML, but the XML is dynamic and I wanto to optimize my cycle because I can have a very big and large XML (this is only a little part of It).

$xml_str = file_get_contents('xml/country.xml');
    $xml = new SimpleXMLElement($xml_str);
    echo $xml->getName(), PHP_EOL;
    foreach($xml as $name => $part) {
       echo "$name: $part", PHP_EOL;
    }

I want to create a very otpimize cycle to take my value


回答1:


Use the following style to acces only the <DATA> child elements in the root element:

foreach ($xml->DATA as $name => $part) { 

Also please check the SimpleXML Introduction it should have some good examples for you showing different ways how to do the basic stuff with SimpleXML. The documentation is pretty good.




回答2:


you could use the XML parser directly, this would more than likely be the most efficient way

set_time_limit(0);
define('__BUFFER_SIZE__', 131072);
define('__XML_FILE__', 'pf_1360591.xml');

function elementStart($p, $n, $a) {
  //handle opening of elements
}

function elementEnd($p, $n) {
  //handle closing of elements
}

function elementData($p, $d) {
  //handle cdata in elements
}

$xml = xml_parser_create();

xml_parser_set_option($xml, XML_OPTION_TARGET_ENCODING, 'UTF-8');
xml_parser_set_option($xml, XML_OPTION_CASE_FOLDING, 0);
xml_parser_set_option($xml, XML_OPTION_SKIP_WHITE, 1);

xml_set_element_handler($xml, 'elementStart', 'elementEnd');
xml_set_character_data_handler($xml, 'elementData');

$f = fopen(__XML_FILE__, 'r');
if($f) {
  while(!feof($f)) {
    $content = fread($f, __BUFFER_SIZE__);

    xml_parse($xml, $content, feof($f));

    unset($content);
  }
  fclose($f);
}


来源:https://stackoverflow.com/questions/15273870/read-xml-dynamic-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!