Skip to content
Advertisement

XML file with different elements

Hello i have this xml file

<are:Ares_odpovedi xmlns:are="http://wwwinfo.mfcr.cz/ares/xml_doc/schemas/ares/ares_answer/v_1.0.1" xmlns:dtt="http://wwwinfo.mfcr.cz/ares/xml_doc/schemas/ares/ares_datatypes/v_1.0.4" xmlns:udt="http://wwwinfo.mfcr.cz/ares/xml_doc/schemas/uvis_datatypes/v_1.0.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" odpoved_datum_cas="2022-06-28T13:59:37" odpoved_pocet="1" odpoved_typ="Standard" vystup_format="XML" xslt="klient" validation_XSLT="/ares/xml_doc/schemas/ares/ares_answer/v_1.0.0/ares_answer.xsl" xsi:schemaLocation="http://wwwinfo.mfcr.cz/ares/xml_doc/schemas/ares/ares_answer/v_1.0.1 http://wwwinfo.mfcr.cz/ares/xml_doc/schemas/ares/ares_answer/v_1.0.1/ares_answer_v_1.0.1.xsd" Id="ares">
<are:Odpoved>
<are:Pocet_zaznamu>1</are:Pocet_zaznamu>
<are:Typ_vyhledani>FREE</are:Typ_vyhledani>
<are:Zaznam>
<are:Shoda_ICO>
<dtt:Kod>9</dtt:Kod>
</are:Shoda_ICO>
<are:Vyhledano_dle>ICO</are:Vyhledano_dle>
<are:Typ_registru>
<dtt:Kod>2</dtt:Kod>
<dtt:Text>OR</dtt:Text>
</are:Typ_registru>
<are:Datum_vzniku>2010-11-18</are:Datum_vzniku>
<are:Datum_platnosti>2022-06-28</are:Datum_platnosti>
<are:Pravni_forma>
<dtt:Kod_PF>121</dtt:Kod_PF>
</are:Pravni_forma>
<are:Obchodni_firma>Dermacol, a.s.</are:Obchodni_firma>
<are:ICO>24766208</are:ICO>
<are:Identifikace>
<are:Adresa_ARES>
<dtt:ID_adresy>212020195</dtt:ID_adresy>
<dtt:Kod_statu>203</dtt:Kod_statu>
<dtt:Nazev_okresu>Hlavní město Praha</dtt:Nazev_okresu>
<dtt:Nazev_obce>Praha</dtt:Nazev_obce>
<dtt:Nazev_casti_obce>Ruzyně</dtt:Nazev_casti_obce>
<dtt:Nazev_mestske_casti>Praha 6</dtt:Nazev_mestske_casti>
<dtt:Nazev_ulice>Výtvarná</dtt:Nazev_ulice>
<dtt:Cislo_domovni>1023</dtt:Cislo_domovni>
<dtt:Typ_cislo_domovni>1</dtt:Typ_cislo_domovni>
<dtt:Cislo_orientacni>4</dtt:Cislo_orientacni>
<dtt:PSC>16100</dtt:PSC>
<dtt:Adresa_UIR>
<udt:Kod_oblasti>19</udt:Kod_oblasti>
<udt:Kod_kraje>19</udt:Kod_kraje>
<udt:Kod_okresu>3100</udt:Kod_okresu>
<udt:Kod_obce>554782</udt:Kod_obce>
<udt:Kod_pobvod>60</udt:Kod_pobvod>
<udt:Kod_nobvod>60</udt:Kod_nobvod>
<udt:Kod_casti_obce>400394</udt:Kod_casti_obce>
<udt:Kod_mestske_casti>500178</udt:Kod_mestske_casti>
<udt:PSC>16100</udt:PSC>
<udt:Kod_ulice>507130</udt:Kod_ulice>
<udt:Cislo_domovni>1023</udt:Cislo_domovni>
<udt:Typ_cislo_domovni>1</udt:Typ_cislo_domovni>
<udt:Cislo_orientacni>4</udt:Cislo_orientacni>
<udt:Kod_adresy>22714359</udt:Kod_adresy>
<udt:Kod_objektu>22016511</udt:Kod_objektu>
</dtt:Adresa_UIR>
</are:Adresa_ARES>
</are:Identifikace>
<are:Kod_FU>6</are:Kod_FU>
<are:Priznaky_subjektu>NAAANANNNNANNNNNNNNNNNNNANNNNN</are:Priznaky_subjektu>
</are:Zaznam>
</are:Odpoved>
</are:Ares_odpovedi>

and i need to get data from element <are:ICO> with php, i tried this, but that shows me error, because of that “are:” i think, it shows me this error

“Parse error: syntax error, unexpected token “:” in C:xampphtdocsxmldata.php on line 4″

<?php
$xml = new SimpleXMLElement("https://wwwinfo.mfcr.cz/cgi-bin/ares/darv_std.cgi?ico=24766208", LIBXML_NOCDATA, true);
foreach ($xml->Odpoved as $result) {
    $ico = $result->are:ICO;
}
echo $ico;
?> 

Advertisement

Answer

Since you are dealing with xml and namespaces, you should parse the data with an xml parser and, using xpath, properly handle the namespaces. Something like:

$url = "https://wwwinfo.mfcr.cz/cgi-bin/ares/darv_std.cgi?ico=24766208";
$xml = simplexml_load_file($url);
$xml->registerXPathNamespace("are", "http://wwwinfo.mfcr.cz/ares/xml_doc/schemas/ares/ares_answer/v_1.0.1");
$ico = $xml->xpath("//are:ICO");
echo $ico[0];

Output:

24766208
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement