Hello all, please bear with me on this as its my first dabble into XML parsing with php I am trying to use miniXML to parse a test xml feed i have created and then read out the results. the code i have is.. <?php $test=" <?xml version=\"1.0\" encoding=\"iso-8859-1\"?> <NewsML> <Catalog Href=\"http://www.afp.com/dtd/AFPCatalog.xml\"/> <NewsEnvelope> <DateAndTime>20040602T085510Z</DateAndTime> </NewsEnvelope> </NewsML> "; header("Content-type: text/html"); // Use the MiniXML library require_once("../xml/includes/minixml.inc.php"); ?> <HTML> <HEAD> <TITLE>MiniXML: Overview</TITLE> </HEAD> <BODY> <?php //Create a MiniXMLDoc object: $parsedDoc = new MiniXMLDoc(); //Call fromString() $parsedDoc->fromString($test); //That's all. Now we can access the elements and their data using appropriate methods. //To access elements, start by getting the root element. $rootEl =& $parsedDoc->getRoot(); $newsml =& $rootEl->getElement('NewsML'); $newsen =& $newsml->getElement('NewsEnvelope'); $status =& $newsml->getElement('DateAndTime'); print $status; ?> </BODY> </HTML> Code (markup): As you can see its pretty basic, but all it outputs is the word object as seen here http://www.weblinker.co.uk/xml/index1.php. what does that mean and can anyone offer any suggestions? thanks Mike
I'm not familiar with MiniXML, but as I can see you are outputing an object type variable there. So, if you want to examine the object use var_dump($status). However, I belive this is not what you're looking for and I know the $parsedDoc instance supports the toString() method, so I'd test: echo $parsedDoc->toString(); Code (markup): I don't know if the element objects support this method (eg. $status->toString()) but you should try it out. Good luck!
thanks Evoleto, That really helped me take a step forward and i am now on my way to getting it!! One other question i have is, is there a php way of getting the xml from a particular url and putting it into a file, so i can manipulate it as above? Thanks again i really appreciate it Mike
I'm glad I could help Mike. Yes you can open remote XML files directly via fopen(): $fp = fopen("http://www.here.com/news.xml", "r"); Code (markup): Or directly store the feed contents in a string variable: $myxml = file_get_contents("http://www.here.com/news.xml"); Code (markup):
Hmm, just a guess but I think the second argument of your file_get_contents is wrong; in any case it's not needed.
Ipheo, Thanks for pointing out, I just copy/pasted the fopen example and forgot to remove the open type argument.
hmm this is wierd, it is almost working fine, however, It is failing to display seemingly random bits of the feed. Infact having used the echo $parsedDoc->toString(); Code (markup): mentioned above i can see that it is not even there? any ideas, or even better any very basic parsing scripts that i can play with?