2

Its my first time working with an XML file and I am using SimpleXML.

I am under the impression that doing $xml->dni-listings->get-listings-by-day->current->date is possible? but I dont know how to do it. Currently the only way that I managed to do it is with the following code. but i find it too long winded for something that, surely, should be more simple?

My XML file is here http://www.dmax.it/tvl-fe/day/?type=day&channel_code=DXIT-IT&filter=1130&date=01052013

enter image description here

$file = file_get_contents ('http://www.dmax.it/tvl-fe/day/?type=day&channel_code=DXIT-IT&filter=1130&date=01052013');
$xml = new  SimpleXMLElement ($file);

foreach($xml->children()->children() as $child)
  {
    echo $child->getName() . "<br />";
    if ('current' == $child->getName()){
        echo $child->date . "<br />";
    }
}
Jonathan Thurft
  • 4,087
  • 7
  • 47
  • 78
  • Just some reference (I've seen you so far solved it already but it can be interesting nevertheless): [Using XML node names with hyphens in PHP](http://stackoverflow.com/q/3634599/367456) and the famous [*SimpleXML basic usage examples*](http://php.net/simplexml.examples-basic) – hakre May 01 '13 at 18:05

3 Answers3

1

Ok, after a lot of trial and error I found the solution.

Apparently there are several ways to get to the child elements one is by invoking children() this would then make available all children at that level.

This is how I got the date:

$xml->children()->children()->current->date;

and for the hyphen attributes this is how I accessed them:

$xml->children()->children()->{'previous-date'}->formatted;

Also I found this other way of doing this

$namespacesMeta = $xml->getNamespaces(true);
$xml->children(@$namespacesMeta['dni-listings'])->children(@$namespacesMeta['get-listings-by-day'])->children(@$namespacesMeta['current'])->date;

I putted the @ before the variable because other wise I get this Notice: Undefined index: get-listings-by-day in C:\xampp\htdocs\test.php on line 8 but this is another way of doing this

the issue with my original problem is due to the fact that things were hyphen and my path should have looked more like

$xml->{'get-listings-by-day'}->current->date 
Jonathan Thurft
  • 4,087
  • 7
  • 47
  • 78
  • 1
    You get the warnings because there are not these namespaces. Instead of hiding the warning away (`@`-operator), fix the cause ;) Just do not use namespaces where there are none ;) – hakre May 01 '13 at 18:06
1

$xml itself represents the root-node, in your case <dni-listings>, so leave that one out in the path:

$dates = $xml->{'get-listings-by-day'}->current->date

will give you the dates. Display all dates:

foreach ($dates as $date) echo $date;

see it working: http://codepad.viper-7.com/xTyeML

michi
  • 6,565
  • 4
  • 33
  • 56
  • Your answer seems legit. It also show Jonathan how to compact the code and preventing him from wrongly using namespaces here (because there are no namespaces at all in the document). Counter upvote has been unlocked. – hakre May 01 '13 at 18:04
0

Note that XML::Simple is not recommended for new applications.

I access items using Perl's hashes. For simple cases I use:

$xml = new  SimpleXMLElement ($file, keyattr => [] );
$xml->{dni-listings}->{get-listings-by-day}->{current}->{date};

If you want to write the XML back out and not have it fold things like into a single tag, you may need to use the ForceArray option. This is a bit harder but works well.

$xml = new  SimpleXMLElement ($file, ForceArray => 1, keyattr => [] );
$xml->{dni-listings}[0]->{get-listings-by-day}[0]->{current}[0]->{date}[0];
Wadester
  • 343
  • 2
  • 5