Hi, maybe it knows how to parse XML
- one. $ xml = new \ SimpelXMLElement ($ response); - etki
- And in the example you can? - jikol
- onePHP has libraries for working with XML, you just have to open the documentation. There and SAX, and DOM, and SimpleXML, and XPath, the choice for every taste and color. - Alex Krass
- Unfortunately, I'm not very good at writing scripts, so I asked for an example. I can only correct and so on. Can I give an example on my question? Thanks - jikol
- Then the question is, what do you want to get as a result of parsing? - Alex Krass
2 answers
the right approach: we do simplexml_load_file or simplexml_load_string (if, say, re-encoded xml in php, we can load from a string)
if everything went well, we get an xml-object, with it we can work using xpath , the path to the elements.
for example: create an array of handler functions: for each matching xpath, one of these functions will be called
$xPaths=array( '/mynode/keywords/location[@keywords]'=>'', //ничего не делаем '/mynode/filters/filter'=>'filter', //вызовем функцию filter() '/mynode/someStrangeThing'=>'someStrangeThing', //функцию someStrangeThing() '/mynode/pid'=>'pid' //pid() ); further work, for example, like this:
foreach ($xPaths as $path => $setting) { $pathResult = $xml->xpath($path); if ($pathResult) { if (!isset($foundPaths[$path])) { $foundPaths[$path] = array(); } $func = trim(preg_replace("/\W+/", "_", $path), "_"); $tagResult = $func($pathResult); //вызываем функцию-обработчик if ($tagResult) { $foundPaths[$path][$oid] = $tagResult; // меняем элемент } } } this is my example, you do not need to use arrays of functions, etc., I just wanted to show you an example of using xpath, and not stupid and blind foreach for all cases
The easiest way is to use the function simplexml_load_file() , which returns a SimpleXMLElement object with the structure of an XML file
<?php $url = 'https://hexillion.com/samples/WhoisXML/?query=google.com'; $xml = simplexml_load_file($url); echo '<pre>'; echo $xml->QueryResult->WhoisRecord->RawText;