PHP 想要寫 XML、OPML 檔,然後內容可以一層一層加入,可以使用 SimpleXMLElement 來達成~
PHP 寫 XML、OPML 的簡易範例
PHP 要產生 XML 檔,範例程式如下:
- <?php
- $xml_file = 'test.xml';
- $xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><opml version="2.0"></opml>');
- $head = $xml->addChild('head');
- $head->addChild('title', '這是標題');
- $head->addChild('producer', 'Tsung');
- $head->addChild('version', '1.0.0');
- $body = $xml->addChild('body');
- $root_outline = $body->addChild('outline');
- $root_outline->addAttribute('text', 'ABC');
- $root_outline = $body->addChild('outline');
- $root_outline->addAttribute('text', 'DEF');
- $outlineL2 = $rootOutline->addChild('outline');
- $outlineL2->addAttribute('text', 'AAA');
- $xml->asXML($xml_file); // write to file
- ?>
執行後,會產生 test.xml,內容如下:
<?xml version="1.0" encoding="UTF-8"?> <opml version="2.0"><head><title>這是標題</title><producer>Tsung</producer><version>1.0.0</version></head><body><outline text="ABC"/><outline text="DEF"/></body></opml>