开发者

How do you work with XML in PHP?

I need to use XML to provide data to a gateway. I'm wondering what is the best way to "construct" an XML tree from PHP.

I though to do something like a template file (because it is always the same tree with variable parameters) and then use str_replace() or regex or something like this to populate my tree.

But since there are tools to w开发者_如何学运维ork with XML in PHP, I guess it may not be the best way to do. Any ideas?


SimpleXML is probably the best choice, in terms of simplicity:

$xml = new SimpleXMLElement('<root />');
$child = $xml->addChild('child');
$child->addChild('name', 'Tim');

echo $xml->asXML();

// Echoed result:
//
// <?xml version="1.0"?> 
// <root>
//   <child>
//     <name>Tim</name>
//   </child>
// </root>


I am usually taking cherry pudding, but since there are coconut cookies I guess it may not be the best way to do.

I see no point in seeking "probably best way" if your current one suits you well.
It's kind of premature optimization. Which is well-known root of all evil.

In fact, a template file being excellent solution: familiar, readable, supportable and such.

XML is the same structured language as HTML. So, you can use whatever templating engine, including PHP itself.

Any reason you don't want to have such a neat template?

<rss version="2.0">
  <channel>
    <title><?=$t['title']?></title>
    <link><?=$t['link']?></link>
    <description><?=$t['desc']?></description>
    <language>en</language>
    <pubDate><?=$t['pubdate']?></pubDate>
     <lastBuildDate><?=$t['lbdate']?></lastBuildDate>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <generator>My RSS generator</generator>
    <managingEditor><?=$t['email']?></managingEditor>
    <webMaster><?=$t['email']?></webMaster>
<? foreach ($data as $row): ?>
    <item>
      <title><?=$row['name']?></title>
      <link><?=$row['link']?></link>
      <description><?=$row['desc']?></description>
      <pubDate><?=$row['pubdate']?></pubDate>
      <guid><?=$row['guid']?></guid>
    </item>
<? endforeach ?>
  </channel>
</rss>


SimpleXML is a really easy to use php library but it a bit slow, too. For complex xml manipulations I like to use FluentDOM, it brings a jquery like api to php and allows something like:

$fd = new FluentDOM();
$fd->contentType = 'html';

$menu = $fd->append('<ul/>');

// first menu item
$menu
  ->append('<li/>')
  ->append('<a/>')
  ->attr('href', '/sample.php')
  ->text('Sample');

// second menu item
$menu
  ->append('<li/>')
  ->append('<a/>')
  ->attr('href', 'http://fluentdom.org')
  // add a class to the menu item <a>
  ->addClass('externalLink')
  ->text('FluentDOM');

FluentDOM is a bit faster than SimpleXML, because it uses the native DOM extension of php5. But the fastest way to create simple xml strings is concatenation and sprintf.

$xml = "<root>";
$xml .= "<child>";
$xml .= sprintf("<name>%s</name>", $name);
$xml .= "<child>";
$xml .= "</root>";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜