D
D
Dublerr2019-09-03 12:14:48
PHP
Dublerr, 2019-09-03 12:14:48

How to change the simplest parser for xml?

Good afternoon dear community.
Please tell me how to make changes to the simplest parser in order to select the necessary elements from the .xml file? I am not a programmer, the parser code was taken after some searches on the php.net resource , but this is where my "knowledge" in the field of php ended ((
I need to display the results of sports competitions of sports school students for several years, so it is desirable that the parser code be minimal All other tasks are solved, only the implementation of this function is required. The database is already formed, so it is not possible to make changes.
Below is an example of the source file and the parser code, as well as the desired results. Thank you in advance for your help.
Source:
https://site.ru/source_file.xml

<puple>
<dlina_puple col_popytok="7">2.95</dlina_puple>
<vysota_puple col_popytok="4">1.35</vysota_puple>
</puple>

Parser:
<?php
/* %%%%%%%%%%%%%%%%%% */
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://site.ru/source_file.xml");
curl_setopt($ch, CURLOPT_HEADER, false);

curl_exec($ch);

curl_close($ch);
/* %%%%%%%%%%%%%%%%%% */
?>

How it outputs:
2.951.35
How it should:
2.95
1.35
As ideally (but not necessarily):
2.95 / 7
1.35 / 4
Regards,
Yuriy

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Matveev, 2019-09-16
@Dublerr

$url = "https://site.ru/source_file.xml";
if ($response = file_get_contents($url) and !empty($response) and $xml = new SimpleXMLElement($response)) {
    echo $xml->dlina_puple . " / " .  (int)$xml->dlina_puple[0]->attributes()->col_popytok . "<br />";
    echo $xml->vysota_puple . " / " .  (int)$xml->vysota_puple[0]->attributes()->col_popytok . "<br />";
} else {
    echo 'Ошибка открытия файла XML';
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question