[development] trouble destructing xml

Matt matt at aleph-null.tv
Fri Mar 25 02:20:06 UTC 2011


I'm serious, this is exactly what QueryPath was written for. The
QueryPath code for what you are asking would be:

foreach (qp('text.xml', 'cn') as $cn) {
  // print an attr
  dpm('doc attr: ' . $cn->attr('doc'));

  // print the PCDATA text
  dpm('text: ' . $cn->text());
}

Since QueryPath takes any old CSS3 selectors, you could do more
complicated queries if you wanted. For example, if you only wanted CN
elements where the doc attr was true, you'd just do this:

foreach (qp('text.xml', 'cn[doc=true]') as $cn) {
  dpm('text: ' . $cn->text());
}

Not to mention that QueryPath is actually faster than most SimpleXML
operations because it traverse the DOM tree more efficiently.

And you can download QueryPath just like any other Drupal module.

Honestly, if you're not keen on the QueryPath idea, you should just
use PHP's DOM interface. It's more efficient than SimpleXML, which is
a HORRIBLE data structure for working with XML anyway. It's
inefficient and it's full of the sorts of oddities that you are
describing right now.

On Thu, Mar 24, 2011 at 8:58 PM,  <jeff at ayendesigns.com> wrote:
> Thanks for the replies...but no joy yet. The XML data looks like this (I'm
> minimizing it)
>
> <?xml version="1.0" encoding="UTF-8" standalone="no"?>
> <document>
>     <channels>
>         <channel>
>             <episode>
>                 <episode-content>
>                     <doc>
>                         <cncpts>
>                             <cn doc="true" er="164" kw="false" ne="true"
> sc="9.647706" sr="164" t="unk">here I am</cn>
>
> and I have
>
>     $t = simplexml_load_file('text.xml','SimpleXMLElement', LIBXML_NOCDATA);
>     dsm($t);
>     foreach
> ($t->channels->channel->nlp->episode->{episode-content}->doc->cncpts->cn as
> $k) {
>         $dsm($k);
>     }
>
> The first dsm immediately after loading the file does not show the seven
> 'cn' elements that I have, and the second is empty because the foreach
> complains.
>



-- 
http://technosophos.com
http://querypath.org


More information about the development mailing list