0

可能重复:
让 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"xml 标头中添加类似的属性,这样我就可以拥有这样的东西:

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

我不想在输出文件上使用查找和替换之类的东西。

4

1 回答 1

-3

读这个: -

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

尝试这个:-

Example #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();
?>
于 2012-07-30T14:27:59.617 回答