国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home php教程 PHP源碼 4 ways to generate XML files in PHP

4 ways to generate XML files in PHP

Nov 09, 2016 am 10:06 AM

4 ways to generate XML files in PHP

<?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>

【Generate string directly】
Method 1: Use pure PHP code to generate a string and write this string to a file with XML as the suffix. This is the most primitive method of generating XML, but it works!
The PHP code is as follows:

<?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;
 
//  創(chuàng)建XML單項(xiàng)
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;
}
 
?>

【DomDocument】
Method 2: Use DomDocument to generate XML files
Create nodes using the createElement method,
Create text content using the createTextNode method,
Add child nodes using the appendChild method,
Create attributes using createAttribute Method
PHP code is as follows:

<?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;,
    )
);
 
//  屬性數(shù)組
$attribute_array = array(
    &#39;title&#39; => array(
    &#39;size&#39; => 1
    )
);
 
//  創(chuàng)建一個(gè)XML文檔并設(shè)置XML版本和編碼。。
$dom=new DomDocument(&#39;1.0&#39;, &#39;utf-8&#39;);
 
//  創(chuàng)建根節(jié)點(diǎn)
$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) {
            //  創(chuàng)建元素
            $$key = $dom->createElement($key);
            $item->appendchild($$key);
 
            //  創(chuàng)建元素值
            $text = $dom->createTextNode($val);
            $$key->appendchild($text);
 
            if (isset($attribute[$key])) {
            //  如果此字段存在相關(guān)屬性需要設(shè)置
                foreach ($attribute[$key] as $akey => $row) {
                    //  創(chuàng)建屬性節(jié)點(diǎn)
                    $$akey = $dom->createAttribute($akey);
                    $$key->appendchild($$akey);
 
                    // 創(chuàng)建屬性值節(jié)點(diǎn)
                    $aval = $dom->createTextNode($row);
                    $$akey->appendChild($aval);
                }
            }   //  end if
        }
    }   //  end if
}   //  end function
?>

[XMLWriter]
Method 3: Use XMLWriter class to create XML files
This method is valid after PHP 5.1.2
In addition, it can output multiple encodings of XML, but the input can only It is utf-8
The PHP code is as follows:

<?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;,
    )
);
 
//  屬性數(shù)組
$attribute_array = array(
    &#39;title&#39; => array(
    &#39;size&#39; => 1
    )
);
 
$xml = new XMLWriter();
$xml->openUri("php://output");
//  輸出方式,也可以設(shè)置為某個(gè)xml文件地址,直接輸出成文件
$xml->setIndentString(&#39;  &#39;);
$xml->setIndent(true);
 
$xml->startDocument(&#39;1.0&#39;, &#39;utf-8&#39;);
//  開(kāi)始創(chuàng)建文件
//  根結(jié)點(diǎn)
$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) {
              //  設(shè)置屬性值
                    $xml->writeAttribute($akey, $aval);
                }
 
            }
 
            $xml->text($row);   //  設(shè)置內(nèi)容
            $xml->endElement(); // $key
        }
 
    }
    $xml->endElement(); //  item
}
 
$xml->endElement(); //  article
$xml->endDocument();
 
$xml->flush();
?>

[SimpleXML]
Method 4: Use SimpleXML to create an XML document

<?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;,
    )
);
 
//  屬性數(shù)組
$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) {
             //  設(shè)置屬性值
                  $node->addAttribute($akey, $aval);
            }
          }
        }
    }
}
echo $xml->asXML();
?>
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

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)