Reputation: 327
I am having problems loading an XML feed with a php script. I make a copy of the feed on my website and load that so I know what I am debugging. Every single entry in the file has a name space(there are several different ones.)
This is my first time messing with name spaces and I am not able to even get the file to load. When I try
$xml = new SimpleXMLElement($result) or die("Error: Cannot create object\n<hr>".$result);
It fires the error. That has worked for a dozen different feeds that do not have name spaces(or at least I have not noticed any name spaces). I have tried various ways to get more info on why it can not create the simpleXMLElement and so far none of them return a problem.
I have valided the xml feed with 4 different online validaters. 3 appear to use javascript and say that the feed is perfect, the 4th http://www.xmlvalidation.com/ uploads the code to its site and returns saying there was a error with their site.(With other xml their validator works just fine.)
I think I have narrowed the problem down to the Name space stuff. If I remove the name space stuff at the top I can load the xml file with the above code, and then I get a mess of name space errors. Below you can see the namespace info.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns1:timeSeriesResponse xsi:schemaLocation="http://www.cuahsi.org/waterML/1.1/ http://waterservices.usgs.gov/WaterML-1.1.xsd" xmlns:ns1="http://www.cuahsi.org/waterML/1.1/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ns1:queryInfo xmlns:ns2="http://www.cuahsi.org/waterML/1.1/">
Link to a sample xml page http://waterservices.usgs.gov/nwis/iv/?format=waterml,1.1&sites=06306300¶meterCd=00060,00065
Upvotes: 1
Views: 115
Reputation: 2347
This should work:
$xml = new SimpleXMLElement($your_xml_data, 0, false, 'ns1', true);
Upvotes: 1
Reputation: 7896
try below solution by using different namespaces.
$xml_element = simplexml_load_file('http://waterservices.usgs.gov/nwis/iv/?format=waterml,1.1&sites=06306300¶meterCd=00060,00065');
$name_spaces = $xml_element->getNamespaces(true);
print_r($name_spaces);
$withns1 = $xml_element->children($name_spaces['ns1']);
$withns2 = $xml_element->children($name_spaces['ns2']);
$withxsi = $xml_element->children($name_spaces['xsi']);
var_dump($withns1);
var_dump($withns2);
var_dump($withxsi);
variable $name_spaces
will have all namespaces list in the xml document.
output of print_r($name_spaces);
will be
Array
(
[ns1] => http://www.cuahsi.org/waterML/1.1/
[xsi] => http://www.w3.org/2001/XMLSchema-instance
[ns2] => http://www.cuahsi.org/waterML/1.1/
)
using above method you can get xml object and iterate it for desired output.
Upvotes: 1