read xml file with php
Using DomDocument:
<?php
$str = <<<XML
<currencies>
<currency name="US dollar" code_alpha="USD" code_numeric="840" />
<currency name="Euro" code_alpha="EUR" code_numeric="978" />
</currencies>
XML;
$dom = new DOMDocument();
$dom->loadXML($str);
foreach($dom->getElementsByTagName('currency') as $currency)
{
echo $currency->getAttribute('name'), "\n";
echo $currency->getAttribute('code_alpha'), "\n";
echo $currency->getAttribute('code_numeric'), "\n";
echo "+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+\n";
}
?>
Live DEMO.
Using simplexml:
<?php
$str = <<<XML
<currencies>
<currency name="US dollar" code_alpha="USD" code_numeric="840" />
<currency name="Euro" code_alpha="EUR" code_numeric="978" />
</currencies>
XML;
$currencies = new SimpleXMLElement($str);
foreach($currencies as $currency)
{
echo $currency['name'], "\n";
echo $currency['code_alpha'], "\n";
echo $currency['code_numeric'], "\n";
echo "+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+\n";
}
?>
Live DEMO.
Parse XML file with PHP
$XMLDoc
is of type SimpleXMLElement.
I think you could change this line:
foreach($XMLDoc->Data->Record as $Record)
to this line:
foreach($XMLDoc->Record as $Record)
For example:
foreach($XMLDoc->Record as $Record)
{
echo (string)$Record->Type;
echo (string)$Record->Building_ID;
echo "<br>";
}
Will result in:
BUILDING0000_00
SITE0001
Parse XML file from url using php
Seems like there is some redirection problem in the url you are accessing, and most probably simplexml_load_file()
doesn't follow redirects...
So the solution would be to use file_get_contents()
or cUrl
...
As file_get_contents()
is easier, I am showing that only...
The code would have to be something like:
<?php
$xml = simplexml_load_string(file_get_contents('http://example_page.ugu.pl/api/test.xml'));
?>
More:
<?php
$xml = simplexml_load_string(file_get_contents('http://jakdojade.pl/pages/api/outputs/schedules.xml'));
?>
^ That too, totally works!
Load and parse XML file with PHP
Here's a couple of pointers..
To download a file and save it, the easiest way I have found is this:
<?php
file_put_contents('saved.xml', file_get_contents('http://www.xmlfiles.com/examples/simple.xml'));
You can then open the file with the simpleXML library like so:
$xml = simplexml_load_file('saved.xml');
var_dump($xml);
Hope that gives you enough info to get started.
See simpleXML for info on the simpleXML library.
How to parse xml with php?
You can use SimpleXML_Load_String.
<?php // RAY_temp_burhan.php
error_reporting(E_ALL);
echo '<pre>';
$xml = <<<ENDXML
<?xml version="1.0" encoding="ISO-8859-1" ?>
<eqlist>
<earhquake name="2012.12.31 18:35:13" lokasyon="CAMONU-AKHISAR (MANISA) Ilksel" lat="38.9572" lng="27.8965" mag="2.9" Depth="5.0" />
<earhquake name="2012.12.31 18:54:09" lokasyon="VAN GÖLÜ Ilksel" lat="38.7273" lng="43.1598" mag="2.3" Depth="2.1" />
<earhquake name="2012.12.31 21:00:49" lokasyon="KUCUKESENCE-ERENLER (SAKARYA) Ilksel" lat="40.7347" lng="30.4742" mag="1.9" Depth="4.4" />
</eqlist>
ENDXML;
// CONVERT TO AN OBJECT
$obj = SimpleXML_Load_String($xml);
// PARSE OUT SOME ATTRIBUTES
foreach ($obj as $quake)
{
// ATTRIBUTE NAMES ARE CASE-SENSITIVE
$loc = $quake->attributes()->lokasyon;
$dep = $quake->attributes()->Depth;
echo PHP_EOL . "$loc $dep";
}
How to parse xml file using php script
You need to get an apply the namespace to the children. http://php.net/manual/en/simplexmlelement.getnamespaces.php
<?php
$xml = '<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:g="http://base.google.com/ns/1.0" version="2.0">
<channel>
<title>werwer</title>
<link>werwerwe</link>
<item>
<g:id>704667</g:id>
<title>Nike</title>
<description>erterterter</description>
</item>
<item>
<g:id>4456456</g:id>
<title>Nike</title>
<description>erterterter</description>
</item>
</channel>
</rss>';
$xml = simplexml_load_string($xml);
$ns = $xml->getNamespaces(true);
foreach ($xml->channel->item as $item) {
echo $item->children($ns['g'])->id.PHP_EOL;
}
/*
704667
4456456
*/
https://3v4l.org/t2D84
PHP Import / Parse XML File Content Save To Database
Try this:
foreach ($newXML['sms'] as $attrib) {
$date = $attrib["@attributes"]["date"];
$body = $attrib["@attributes"]["body"];
echo $date . " - " . $body;
}
i can't read and parse xml file in php
Here's one for you:
<?php
$xmldata = file_get_contents("https://egytech4uu.herokuapp.com/data.xml");
$xmlparser = xml_parser_create();
$arr = [];
xml_parse_into_struct($xmlparser, $xmldata, $arr);
xml_parser_free($xmlparser);
print_r($arr);
So what's going there? It returns a flat array, but for each node at least we can see its level. As mentioned in the comments, this is not the best way to parse XML, only a last resort kind of thing.
How to parse a remote XML file with PHP?
I have no idea which information you want to extract so here is an example how to get the attributes 'id' and 'slug' from all market nodes.
Just add compress.zlib://
to your url to get the xml, for PHP 4.3.0 and up
<?php
$xml = simplexml_load_file('compress.zlib://http://smarkets.s3.amazonaws.com/oddsfeed.xml') or die("Error: Cannot create object");
foreach ($xml->event as $item) {
echo $item->market['id'] . "<br>" . $item->market['slug'] . "<br><br>";
}
?>
Related Topics
Session Share Across Multiple Domains on Same Server
Php: Re Order Associative Array
How to Get/Set Session_Id() or Should It Be Generated Automatically
Check If Method Exists in the Same Class
"Transfer-Encoding: Chunked" Header in PHP
Model->Save() Not Working in Yii2
PHP Dynamic Db Page Rewrite Url
Why Does PHP's Call_User_Func() Function Not Support Passing by Reference
Issues Porting PHP/Gd Wrapper to Imagick
Moving from MySQL to MySQLi or Pdo
Error 1366 (Hy000): Incorrect String Value: '\Xf0\X9F\X98\X9C' for Column 'Comment' at Row 1
PHP - Is There a Portable Version of PHPunit
How to Inject a Service in Another Service in Symfony
How to Get Average of Column Values in Laravel
Codeigniter Command Line Error - PHP Fatal Error: Class 'Ci_Controller' Not Found