1

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&parameterCd=00060,00065

Brandan
  • 307
  • 1
  • 4
  • 12
  • what exactly you want to do? only create the SimpleXmlObject? – Chetan Ameta Jan 18 '16 at 09:52
  • I want to process the xml feed, retrieving various data that is useful and then use it to create a page on my website. With feeds from other sources I have created a SimpleXmlObject then used xpath() to find what I am looking for. If there is another better way I am game. – Brandan Jan 18 '16 at 09:56
  • see my answer. Hope it will resolve your issue – Chetan Ameta Jan 18 '16 at 10:00

2 Answers2

1

This should work:

$xml = new SimpleXMLElement($your_xml_data, 0, false, 'ns1', true);
EM-Creations
  • 4,195
  • 4
  • 40
  • 56
Paflow
  • 2,030
  • 3
  • 30
  • 50
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - [From Review](/review/low-quality-posts/10932742) – Michele La Ferla Jan 18 '16 at 11:03
  • I thought the problem was why it was not possible to load the XML data into the simpleXML element and providing the namespace as argument 4 does the trick. – Paflow Jan 18 '16 at 11:38
  • This does answer the question, in fact it is probably the closest to answering the actual question as it uses the same type of code. I am not sure why it should work as there are several namespaces, but it does work. – Brandan Jan 19 '16 at 03:48
1

try below solution by using different namespaces.

$xml_element = simplexml_load_file('http://waterservices.usgs.gov/nwis/iv/?format=waterml,1.1&sites=06306300&parameterCd=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.

Chetan Ameta
  • 7,696
  • 3
  • 29
  • 44
  • `foreach ($withns1->xpath('//ns1:timeSeries') as $Station) ` Would that be the correct way to call xpath with your code? – Brandan Jan 18 '16 at 10:31
  • yes it will work but also depends what type of result you need. better use `foreach ($withns1->timeSeries->children($name_spaces['ns1']) as $Station){` – Chetan Ameta Jan 18 '16 at 10:38
  • for more detail look solutions at http://stackoverflow.com/questions/33994551/php-xml-cant-get-values/33994948#33994948 and http://php.net/manual/en/simplexmlelement.getnamespaces.php – Chetan Ameta Jan 18 '16 at 10:40
  • 1
    I have ended up with `foreach ($withns1->timeSeries as $Station) ` then treat it as normal XML seems to be the easiest way(took a long time to figure out that once the above was done it is then like normal XML except that Xpath is not going to work without some extra work. Now it is starting to click and make sense. – Brandan Jan 19 '16 at 05:53
  • 1
    Note that you don't need to do the `getNamespaces` call other than for debugging; if you're happy to rely on the prefixes `ns1` etc not changing (which they can without changing the meaning of the file!) you can just do `->children('ns1', true)` and so on. Better is to hard-code the actual namespace identifiers (which can't change): `define('XMLNS_WATERML', 'http://www.cuahsi.org/waterML/1.1/'); $with_waterml = $xml_element->children(XMLNS_WATERML);` – IMSoP Jan 19 '16 at 16:40