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

? php教程 php手冊(cè) PHP中的生成XML文件的4種方法分享

PHP中的生成XML文件的4種方法分享

Jun 13, 2016 am 11:57 AM
php xml ?? ???? ?? ?? ?? ???? ~?

生成如下XML串
Xml代碼

復(fù)制代碼 代碼如下:




???
??????? title1
??????? content1
??????? 2009-10-11
???

???
??????? title2
??????? content2
??????? 2009-11-11
???



方法I.【直接生成字符串】
使用純粹的PHP代碼生成字符串,并把這個(gè)字符串寫入一個(gè)以XML為后綴的文件。這是最原始的生成XML的方法,不過有效!

復(fù)制代碼 代碼如下:


$data_array = array(
??? array(
??? 'title' => 'title1',
??? 'content' => 'content1',
??????? 'pubdate' => '2009-10-11',
??? ),
??? array(
??? 'title' => 'title2',
??? 'content' => 'content2',
??? 'pubdate' => '2009-11-11',
??? )
);
$title_size = 1;
$xml = "\n";
$xml .= "

\n";
foreach ($data_array as $data) {
$xml .= create_item($data['title'], $title_size, $data['content'], $data['pubdate']);
}
$xml .= "
\n";
echo $xml;
//? 創(chuàng)建XML單項(xiàng)
function create_item($title_data, $title_size, $content_data, $pubdate_data)
{
??? $item = "\n";
??? $item .= "" . $title_data . "\n";
??? $item .= "" . $content_data . "\n";
??? $item .= " " . $pubdate_data . "\n";
??? $item .= "
\n";
??? return $item;
}
?>


方法2:【DomDocument】
使用DomDocument生成XML文件,創(chuàng)建節(jié)點(diǎn)使用createElement方法,創(chuàng)建文本內(nèi)容使用createTextNode方法,添加子節(jié)點(diǎn)使用appendChild方法,創(chuàng)建屬性使用createAttribute方法

復(fù)制代碼 代碼如下:


$data_array = array(
??? array(
??? 'title' => 'title1',
??? 'content' => 'content1',
??????? 'pubdate' => '2009-10-11',
??? ),
??? array(
??? 'title' => 'title2',
??? 'content' => 'content2',
??? 'pubdate' => '2009-11-11',
??? )
);
//? 屬性數(shù)組
$attribute_array = array(
??? 'title' => array(
??? 'size' => 1
??? )
);
//? 創(chuàng)建一個(gè)XML文檔并設(shè)置XML版本和編碼。。
$dom=new DomDocument('1.0', 'utf-8');
//? 創(chuàng)建根節(jié)點(diǎn)
$article = $dom->createElement('article');
$dom->appendchild($article);
foreach ($data_array as $data) {
??? $item = $dom->createElement('item');
??? $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
?>


方法3:【XMLWriter】
使用XMLWriter類創(chuàng)建XML文件,此方法在PHP 5.1.2后有效。另外,它可以輸出多種編碼的XML,但是輸入只能是utf-8

復(fù)制代碼 代碼如下:


$data_array = array(
??? array(
??? 'title' => 'title1',
??? 'content' => 'content1',
??????? 'pubdate' => '2009-10-11',
??? ),
??? array(
??? 'title' => 'title2',
??? 'content' => 'content2',
??? 'pubdate' => '2009-11-11',
??? )
);
//? 屬性數(shù)組
$attribute_array = array(
??? 'title' => array(
??? 'size' => 1
??? )
);
$xml = new XMLWriter();
$xml->openUri("php://output");
//? 輸出方式,也可以設(shè)置為某個(gè)xml文件地址,直接輸出成文件
$xml->setIndentString('? ');
$xml->setIndent(true);
$xml->startDocument('1.0', 'utf-8');
//? 開始創(chuàng)建文件
//? 根結(jié)點(diǎn)
$xml->startElement('article');
foreach ($data_array as $data) {
??? $xml->startElement('item');
??? 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();
?>


方法4: 【SimpleXML】
使用SimpleXML創(chuàng)建XML文檔

復(fù)制代碼 代碼如下:


$data_array = array(
??? array(
??? 'title' => 'title1',
??? 'content' => 'content1',
??????? 'pubdate' => '2009-10-11',
??? ),
??? array(
??? 'title' => 'title2',
??? 'content' => 'content2',
??? 'pubdate' => '2009-11-11',
??? )
);
//? 屬性數(shù)組
$attribute_array = array(
??? 'title' => array(
??? 'size' => 1
??? )
);
$string =



XML;
$xml = simplexml_load_string($string);
foreach ($data_array as $data) {
??? $item = $xml->addChild('item');
??? 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();
?>

? ????? ??
? ?? ??? ????? ???? ??? ??????, ???? ?????? ????. ? ???? ?? ???? ?? ??? ?? ????. ???? ??? ???? ???? ??? ?? admin@php.cn?? ?????.

? AI ??

Undresser.AI Undress

Undresser.AI Undress

???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover

AI Clothes Remover

???? ?? ???? ??? AI ?????.

Video Face Swap

Video Face Swap

??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

???

??? ??

???++7.3.1

???++7.3.1

???? ?? ?? ?? ???

SublimeText3 ??? ??

SublimeText3 ??? ??

??? ??, ???? ?? ????.

???? 13.0.1 ???

???? 13.0.1 ???

??? PHP ?? ?? ??

???? CS6

???? CS6

??? ? ?? ??

SublimeText3 Mac ??

SublimeText3 Mac ??

? ??? ?? ?? ?????(SublimeText3)

???

??? ??

?? ????
1784
16
Cakephp ????
1729
56
??? ????
1580
28
PHP ????
1444
31
???
??? ??? ?? ?? : PHP ??? ??? ??? ?? ?? : PHP ??? Jul 15, 2025 am 02:48 AM

phphasthreecommentstyles : //, #forsingle-lineand/.../formulti-lline.usecommentstoexplainwhycodeexists, notwhatitdoes.marktodo/fixMeitemsandDisableCodeTemporlinlyDuingDeBugging.aVoidOver-commentingsimplOgic.writeCoCoCoCoCoConcomeCOCOCOCONCOCOCOCOCOCOCOCOCISE

Windows? PHP? ???? ?? Windows? PHP? ???? ?? Jul 15, 2025 am 02:46 AM

Windows? PHP? ???? ?? ???? ??? ?????. 1. ??? PHP ??? ?????? ?? ??????. Apache? ?? ThreadSafe ??? ????? Nginx??? ThreadSafe ??? ???? ?? ????. 2. php.ini ??? ???? php.ini-development ?? php.ini-production? php.ini? ?????. 3. ?? ? ??? ?? ??? ?? ?? ??? PHP ??? ??????. 4. PHP? ????? ?????? ??? ????? ?? ?? ?? PHP-V? ???? ?? ??? ???? ?? ?? ??? ??????. 5. Apache? ???? ?? httpd.conf?? p? ???????.

PHP ?? : ?? PHP ?? : ?? Jul 15, 2025 am 02:46 AM

PHP? ?? ???? 4 ?? ?? ??? ?????. 1. PHP ??? ?????? ??? ??? ???? ?? ?????. 2. Echo ? Print? ????? ?? ???? ????, ??? Echo? ?? ?? ??? ?????? ??????. 3. ?? ???? ?? ???? ??????? //, # ? //; 4. ? ??? ?????? ????? ??? ?? ????? ??? ??? ??? ?? ???? ??? ????. ??? ?? ??? ????? ???? ???? PHP ??? ???? ? ??? ? ? ????.

PHP ? ???? ??? ?????? PHP ? ???? ??? ?????? Jul 16, 2025 am 03:45 AM

phpisaserver-sideScriptingLanguageUsedForWebDevelopment, ?? ProcessesData, InteractSwithDatabases ? SendShtmlTobrowsers.commonusesincludeusera-sectentication, e-commerceplatforms

PHP 8 ?? ??? PHP 8 ?? ??? Jul 16, 2025 am 03:41 AM

???? PHP8? ???? ??? ??? ????. 1. ????? ??? ?? ????; 2. PHP8 ? ?? ?? ??? ??????. 3. ??? ????? ????? ??? ??????. 4. ??? ?? ?? ??? ??????. Windows ???? Zip ???? ?????? ?? ?? ? ?? ?? ??? ???? ???? ????? ?? ??? ??? ?? ? ? ????. MACOS ???? Homebrew? ???? ? ??, PHP8 ??, ?? ?? ?? ? ?? ??? ?? ??? ???? ?? ????. ?? ??? ?? ????? ???? ????? ????? ??? ?? ??? ??? ??? ? ????.

? ?? PHP ???? : ???? ?? ? ?? PHP ???? : ???? ?? Jul 16, 2025 am 03:42 AM

? ?? PHP ???? ??? ???? ??? ?????? ?? ?? ?? ??? ???? XAMPP/MAMP/LAMP? ?? ? ?? ??? ???? ???? ??? ???? ??? ?????. ??, hello.php?? ??? ??? ?? ??? ???? ???? ??????. ??, PHP ? HTML? ???? ?? ??? ??? ???? ?? ????. ?????, ???? ??, ?? ?? ? ?? ?? ??? ?? ???? ?????? ???? ???? ?? ????? ???????.

PHP?? ?? ?? (??/??)? ??? ?????? PHP?? ?? ?? (??/??)? ??? ?????? Jul 16, 2025 am 03:48 AM

ToHandleFileOperationsInphp, useAppreptFunctionsandModes.1.TOREADAFILE, USEFILE_GET_CONTENTS () FORSMALLFILESORFGETS () inALOOPFORLE-by-lineProcessing.2.TOWRITETOAFILE, USEFILE_PUTE_CONTENTS () USEFILE_PUTE_CONTENTS () FORSIMPLEWRITE () FORSIMPLAGFILE (ORFENDFLAG)

?? ??? ?? ??? ?? ??? ?? ??? Jul 15, 2025 am 02:55 AM

Python? Ifelse ??? ???? ??? ??? ??? ?? ??? ???? ????. 1. ???? ??? ???? ??? ???? ????. ??? ??? ?? ??? ???? ?? ?????. 2. ?? ?? ??? ELIF? ?? ????, ????? ???? ?? ???? ?????. 3. ?? ?? ??? ???? ?? ?? ? ?? ? ?? ???? ?? ?? ????. 4. 3 ?? ??? ??? ?????? ??? ifelse? ???? ? ??? ? ????. ?? ??, ??? ?? ? ??? ??????? ?????? ??? ???? ???? ?? ??? ??? ? ????.

See all articles