繁体   English   中英

如何将已解析的数据转换为xml格式

[英]how to convert a parsed data into an xml format

我想使用以下代码使用“ simple html dom parser ”解析HTML内容(网站):

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Parsing</title>
</head>
<body>
    <h1>Démonstration parsing </h1>
    <?php
    require_once 'simple_html_dom.php';
    $html = new simple_html_dom();
    $html->load_file('http://grafikart.fr/blog/');
    // echo $html->find('img',0)->getAttribute('src');
    /*foreach ($html->find('img') as $img) {
        echo $img->src . '<br/>';
    }*/
 foreach ($html->find('.posts') as $post){
     echo 'ARTICLE : '.$post->find('posts-short>p',0)->plaintext.'<br/>';
     echo 'Image de l\'article: '.$post->find('img',0)->src.'<br/>';         
 }
    ?>

</body>
</html>

一切正常,但我想将解析后的数据转换为xml格式。 有人有主意吗? 谢谢!

您可以使用SimpleXML

这是一个简单的示例:

<?php

    $test_array = array (
      'bla' => 'blub',
      'foo' => 'bar',
      'another_array' => array (
        'stack' => 'overflow',
      ),
    );

    $xml = new SimpleXMLElement('<root/>');
    array_walk_recursive($test_array, array ($xml, 'addChild'));
    print $xml->asXML();
?>

结果是

*<?xml version="1.0"?>
<root>
  <blub>bla</blub>
  <bar>foo</bar>
  <overflow>stack</overflow>
</root>*

这里是( 12 ,它使用)的例子Simple HTML DOMSimpleXML

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM