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 />";
    }
}
1

3 Answers 3

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 
Sign up to request clarification or add additional context in comments.

1 Comment

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 ;)
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

1 Comment

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.
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];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.