what am i doing wrong, php xml parsing

Discussion in 'PHP' started by billybrag, Jun 27, 2006.

  1. #1
    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
     
    billybrag, Jun 27, 2006 IP
  2. Evoleto

    Evoleto Well-Known Member

    Messages:
    253
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    108
    #2
    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!
     
    Evoleto, Jun 27, 2006 IP
    billybrag likes this.
  3. billybrag

    billybrag Peon

    Messages:
    324
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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
     
    billybrag, Jun 27, 2006 IP
  4. Evoleto

    Evoleto Well-Known Member

    Messages:
    253
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    108
    #4
    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):
     
    Evoleto, Jun 27, 2006 IP
  5. ipheo.com

    ipheo.com Peon

    Messages:
    21
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    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.com, Jun 27, 2006 IP
  6. Evoleto

    Evoleto Well-Known Member

    Messages:
    253
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    108
    #6
    Ipheo,

    Thanks for pointing out, I just copy/pasted the fopen example and forgot to remove the open type argument.
     
    Evoleto, Jun 27, 2006 IP
  7. billybrag

    billybrag Peon

    Messages:
    324
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #7
    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?
     
    billybrag, Jun 29, 2006 IP