2012-07-30 58 views
0

添加屬性的XML頭可能重複:
Getting SimpleXMLElement to include the encoding in output如何使用的SimpleXMLElement

PHP:

$xml = new SimpleXMLElement("<Title0></Title0>"); 
$xml->Title1='Some Text 1'; 
$output = $xml->asXML('mak.xml'); 

XML輸出:

<?xml version="1.0"?> 
<Title0> 
    <Title1>Some Text 1</Title1> 
</Title0> 

但是我要像encoding="utf-8"添加attrbute到XML頭,讓我能有這樣的事情:

<?xml version="1.0" encoding="utf-8" ?> 
<Title0> 
    <Title1>Some Text 1</Title1> 
</Title0> 

我不想使用查找和替換類的東西對輸出文件。

+3

[這個問題](HTTP://計算器。 com/questions/869650/getting-simplexmlelement-include-the-encoding-in-output)可能會有所幫助。 – Utkanos 2012-07-30 13:44:13

+0

@Utkanos謝謝:) – Makesh 2012-07-30 13:54:41

回答

-3

閱讀本: -

http://php.net/manual/en/simplexml.examples-basic.php

嘗試: -

實施例#10添加的元素和屬性

<?php 
include 'example.php'; 
$movies = new SimpleXMLElement($xmlstr); 

$character = $movies->movie[0]->characters->addChild('character'); 
$character->addChild('name', 'Mr. Parser'); 
$character->addChild('actor', 'John Doe'); 

$rating = $movies->movie[0]->addChild('rating', 'PG'); 
$rating->addAttribute('type', 'mpaa'); 

echo $movies->asXML(); 
?>