2

This is my code so far... needless to say, it's not working :(

$feed = <<< THEXML

<programs>
<program>
<date>2009-04-16</date>
<start_time>17:00</start_time>
<leadtext>hello hello!
</leadtext>
<name>Program 1</name>
<b-line>Comedy</b-line>
<synopsis>Funny stuff
</synopsis>
<url>http://www.domain.tld/program_name</url>
</program>
<programs>
THEXML;


$xml  = (array) simplexml_load_string($feed);

print_r($xml);exit;

Would appreciate any help, have been around the php.net site for hours now and feeling braindead.

Please note that in the example xml above there is just one

 <program>...</program>

tag, but in reality I have one or more of them that I need to use. For example

 <programs>
 <program>...</program>
 <program>...</program>
 <program>...</program>
 </programs>

I figured if I can get one to work then I can loop it, but just thought I would explain what I am going for here.

Thanks in advance!

7
  • What is the output of print_r($xml);? What is keeping you from looping through it? Commented Oct 30, 2012 at 3:17
  • The XML you posted, also, is not valid XML. Commented Oct 30, 2012 at 3:21
  • I am getting errors before it reaches the print_r statement. Unfortunately thats the XML I have to work with... I can add something in the beginning or the end... but the middle rest must be untouched. Commented Oct 30, 2012 at 3:24
  • Ok, warnings.. not errors and the output at the bottom is: Array ( [0] => ) Will post the warnings in a sec Commented Oct 30, 2012 at 3:28
  • Warning: simplexml_load_string() [<a href='function.simplexml-load-string'>function.simplexml-load-string</a>]: Entity: line 17: parser error : Premature end of data in tag programs line 17 in C:\wamp\www\MTG\test_array2.php on line 26 ****** Warning: simplexml_load_string() [<a href='function.simplexml-load-string'>function.simplexml-load-string</a>]: &lt;programs&gt; in C:\wamp\www\MTG\test_array2.php on line 2 Commented Oct 30, 2012 at 3:29

2 Answers 2

2

Assuming the provider gives you valid XML, then you just have to iterate through the programs and use any information you need off it, like so:

<?php
foreach ( $xml as $node ) {
    echo $node->synopsis;
    //or whatever property you want to access on the SimpleXMLElement Object
}

Since you don't have correct XML (being an unclosed <programs> tag, then you might want to modify your script to correct that error like so:

$feed = preg_replace('/<programs>$/', '</programs>', trim($feed));
Sign up to request clarification or add additional context in comments.

4 Comments

The XML will be in the exact same format as I posted it, I can add something in the beginning or the end, but cannot touch the xml that comes in.
Then it's not valid XML, so using an XML parser is not the way to go, specially if you can't correct the errors that are being thrown.
Well, start by correcting the XML by doing what @StRuPs suggests, probably doing something like: $xml = preg_replace('/<programs>$/, '</programs>', trim($xml)); and then feeding the results through simple_xml_load_string();
Yep, that gives me some output...Thanks! I think I can work with the output I am getting now :)
2

You're missing the end closing tag.

$feed = <<< THEXML

<programs>
<program>
<date>2009-04-16</date>
<start_time>17:00</start_time>
<leadtext>hello hello!
</leadtext>
<name>Program 1</name>
<b-line>Comedy</b-line>
<synopsis>Funny stuff
</synopsis>
<url>http://www.domain.tld/program_name</url>
</program>
</programs> // <========= here
THEXML;

5 Comments

I am not good with XML, I see you pointing, but how should it look then? It starts with programs and ends with /programs... what's missing?
That's what gave you the previous error. To transform it into an array, you must iteratively visit every node and its children. I'll grab some code for you. ;)
I kind of see what you mean, I took out the "programS" tags and I got an array... the problem is I may get 1 "program" (no S) or many "program" tags enclosed in a "programS" tag :( OK,Will wait for your response.
I tried using DOMDocument but it did not work. There's this extension Tidy, but the output is not that correct. Get it here: php.net/manual/en/book.tidy.php And try this: // Config $config = array( 'indent' => true, 'input-xml' => true, 'output-xml' => true, 'wrap' => false ); // Tidy $tidy = new tidy(); $tidy->parseString($feed, $config, 'utf8'); $tidy->cleanRepair(); print_r($tidy);
Don't worry about it, I think I can work with Rob's solution as I am getting some output and no errors. Upvoted you, Thanks again.

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.