Here is the very simple way ;)

<?php

$XML=<<<XMLL
<head>
 <a href='/asas' >
   <b>First</b>
  </a>
  <a href='/bla' >
    <b class='klas' >Second</b>
  </a>
</head>
XMLL;


$X = simplexml_load_string($XML);

foreach ($X->a as $a){
        echo $a->b ."\n";
        if(  $a->b['class'] ) {
                echo 'B has class - ' .$a->b['class']."\n";
        }
}

?>





On May 12, 2008, at 1:28 PM, Waynn Lue wrote:

So if I'm looking to parse certain attributes out of an XML tree, if I
use SAX, it seems that I would need to keep track of state internally.
 E.g., if I have a tree like

<head>
 <a>
   <b></b>
  </a>
  <a>
    <b></b>
  </a>
</head>

and say I'm interested in all that's between <b> underneath any <a>,
I'd need to have a state machine that looked for an <a> followed by a
<b>.  If I'm doing that, though, it seems like I should just start
using a DOM parser instead?

Thanks for any insight,
Waynn

On Mon, May 12, 2008 at 1:29 AM, David Otton
<[EMAIL PROTECTED]> wrote:
2008/5/12 Waynn Lue <[EMAIL PROTECTED]>:

What's the best way to pull down XML from a URL?  fopen($URL), then
using xml_parse?  Or should I be using XML_Parser or SimpleXML?

 XML parsers fall into two general camps - DOM and SAX. DOM parsers
 represent an entire XML document as a tree, in-memory, when they are
 first instantiated. They are generally more memory-hungry and take
 longer to instantiate, but they can answer queries like "what is the
 path to this node" or "give me the siblings of this node".

SAX parsers are stream- or event-based, and are much more lightweight - they parse the XML in a JIT fashion, and can't answer much more than
 "give me the next node".

 If you just need the data, a SAX parser will probably do everything
you need. If you need the tree structure implicit in an XML document,
 use a DOM parser. Expat, which XML Parser
 (http://uk3.php.net/manual/en/book.xml.php) is based on, is a SAX
 parser. DOM XML (http://uk3.php.net/manual/en/book.domxml.php) is,
 obviously, a DOM parser. I don't know, off the top of my head, which
 camp SimpleXML falls into.


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Bojan Tesanovic
http://www.carster.us/




Reply via email to