Home > php教程 > PHP源码 > PHP中的生成XML文件的4种方法分享_php技巧

PHP中的生成XML文件的4种方法分享_php技巧

PHP中文网
Release: 2016-05-25 17:14:01
Original
1088 people have browsed it

PHP中的生成XML文件的4种方法分享,需要的朋友可以参考下

生成如下XML串
Xml代码

<?xml version="1.0" encoding="utf-8"?>
<article>
    <item>
        <title size="1">title1</title>
        <content>content1</content>
        <pubdate>2009-10-11</pubdate>
    </item>
    <item>
        <title size="1">title2</title>
        <content>content2</content>
        <pubdate>2009-11-11</pubdate>
    </item>
</article>
Copy after login

方法I.【直接生成字符串】
使用纯粹的PHP代码生成字符串,并把这个字符串写入一个以XML为后缀的文件。这是最原始的生成XML的方法,不过有效!

<?PHP
$data_array = array(
    array(
    &#39;title&#39; => &#39;title1&#39;,
    &#39;content&#39; => &#39;content1&#39;,
        &#39;pubdate&#39; => &#39;2009-10-11&#39;,
    ),
    array(
    &#39;title&#39; => &#39;title2&#39;,
    &#39;content&#39; => &#39;content2&#39;,
    &#39;pubdate&#39; => &#39;2009-11-11&#39;,
    )
);
$title_size = 1;
$xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
$xml .= "<article>\n";
foreach ($data_array as $data) {
$xml .= create_item($data[&#39;title&#39;], $title_size, $data[&#39;content&#39;], $data[&#39;pubdate&#39;]);
}
$xml .= "</article>\n";
echo $xml;
//  创建XML单项
function create_item($title_data, $title_size, $content_data, $pubdate_data)
{
    $item = "<item>\n";
    $item .= "<title size=\"" . $title_size . "\">" . $title_data . "</title>\n";
    $item .= "<content>" . $content_data . "</content>\n";
    $item .= " <pubdate>" . $pubdate_data . "</pubdate>\n";
    $item .= "</item>\n";
    return $item;
}
?>
Copy after login

方法2:【DomDocument】
使用DomDocument生成XML文件,创建节点使用createElement方法,创建文本内容使用createTextNode方法,添加子节点使用appendChild方法,创建属性使用createAttribute方法

<?php
$data_array = array(
    array(
    &#39;title&#39; => &#39;title1&#39;,
    &#39;content&#39; => &#39;content1&#39;,
        &#39;pubdate&#39; => &#39;2009-10-11&#39;,
    ),
    array(
    &#39;title&#39; => &#39;title2&#39;,
    &#39;content&#39; => &#39;content2&#39;,
    &#39;pubdate&#39; => &#39;2009-11-11&#39;,
    )
);
//  属性数组
$attribute_array = array(
    &#39;title&#39; => array(
    &#39;size&#39; => 1
    )
);
//  创建一个XML文档并设置XML版本和编码。。
$dom=new DomDocument(&#39;1.0&#39;, &#39;utf-8&#39;);
//  创建根节点
$article = $dom->createElement(&#39;article&#39;);
$dom->appendchild($article);
foreach ($data_array as $data) {
    $item = $dom->createElement(&#39;item&#39;);
    $article->appendchild($item);
    create_item($dom, $item, $data, $attribute_array);
}
echo $dom->saveXML();
function create_item($dom, $item, $data, $attribute) {
    if (is_array($data)) {
        foreach ($data as $key => $val) {
            //  创建元素
            $$key = $dom->createElement($key);
            $item->appendchild($$key);
            //  创建元素值
            $text = $dom->createTextNode($val);
            $$key->appendchild($text);
            if (isset($attribute[$key])) {
            //  如果此字段存在相关属性需要设置
                foreach ($attribute[$key] as $akey => $row) {
                    //  创建属性节点
                    $$akey = $dom->createAttribute($akey);
                    $$key->appendchild($$akey);
                    // 创建属性值节点
                    $aval = $dom->createTextNode($row);
                    $$akey->appendChild($aval);
                }
            }   //  end if
        }
    }   //  end if
}   //  end function
?>
Copy after login

方法3:【XMLWriter】
使用XMLWriter类创建XML文件,此方法在PHP 5.1.2后有效。另外,它可以输出多种编码的XML,但是输入只能是utf-8

<?php
$data_array = array(
    array(
    &#39;title&#39; => &#39;title1&#39;,
    &#39;content&#39; => &#39;content1&#39;,
        &#39;pubdate&#39; => &#39;2009-10-11&#39;,
    ),
    array(
    &#39;title&#39; => &#39;title2&#39;,
    &#39;content&#39; => &#39;content2&#39;,
    &#39;pubdate&#39; => &#39;2009-11-11&#39;,
    )
);
//  属性数组
$attribute_array = array(
    &#39;title&#39; => array(
    &#39;size&#39; => 1
    )
);
$xml = new XMLWriter();
$xml->openUri("php://output");
//  输出方式,也可以设置为某个xml文件地址,直接输出成文件
$xml->setIndentString(&#39;  &#39;);
$xml->setIndent(true);
$xml->startDocument(&#39;1.0&#39;, &#39;utf-8&#39;);
//  开始创建文件
//  根结点
$xml->startElement(&#39;article&#39;);
foreach ($data_array as $data) {
    $xml->startElement(&#39;item&#39;);
    if (is_array($data)) {
        foreach ($data as $key => $row) {
          $xml->startElement($key);
          if (isset($attribute_array[$key]) && is_array($attribute_array[$key]))
          {
              foreach ($attribute_array[$key] as $akey => $aval) {
              //  设置属性值
                    $xml->writeAttribute($akey, $aval);
                }
            }
            $xml->text($row);   //  设置内容
            $xml->endElement(); // $key
        }
    }
    $xml->endElement(); //  item
}
$xml->endElement(); //  article
$xml->endDocument();
$xml->flush();
?>
Copy after login

方法4: 【SimpleXML】
使用SimpleXML创建XML文档

<?php
$data_array = array(
    array(
    &#39;title&#39; => &#39;title1&#39;,
    &#39;content&#39; => &#39;content1&#39;,
        &#39;pubdate&#39; => &#39;2009-10-11&#39;,
    ),
    array(
    &#39;title&#39; => &#39;title2&#39;,
    &#39;content&#39; => &#39;content2&#39;,
    &#39;pubdate&#39; => &#39;2009-11-11&#39;,
    )
);
//  属性数组
$attribute_array = array(
    &#39;title&#39; => array(
    &#39;size&#39; => 1
    )
);
$string = <<<XML
<?xml version=&#39;1.0&#39; encoding=&#39;utf-8&#39;?>
<article>
</article>
XML;
$xml = simplexml_load_string($string);
foreach ($data_array as $data) {
    $item = $xml->addChild(&#39;item&#39;);
    if (is_array($data)) {
        foreach ($data as $key => $row) {
          $node = $item->addChild($key, $row);
          if (isset($attribute_array[$key]) && is_array($attribute_array[$key]))
            {
              foreach ($attribute_array[$key] as $akey => $aval) {
             //  设置属性值
                  $node->addAttribute($akey, $aval);
            }
          }
        }
    }
}
echo $xml->asXML();
?>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template