Php 如何使用SimpleXMLElement向xml标头添加属性
可能重复:Php 如何使用SimpleXMLElement向xml标头添加属性,php,xml,simplexml,Php,Xml,Simplexml,可能重复: PHP: $xml = new SimpleXMLElement("<Title0></Title0>"); $xml->Title1='Some Text 1'; $output = $xml->asXML('mak.xml'); 我不想在输出文件中使用查找和替换之类的东西。阅读以下内容:-- 试试这个:- 示例#10添加元素和属性 <?php include 'example.php'; $movies = new SimpleX
PHP:
$xml = new SimpleXMLElement("<Title0></Title0>");
$xml->Title1='Some Text 1';
$output = $xml->asXML('mak.xml');
我不想在输出文件中使用查找和替换之类的东西。阅读以下内容:--
试试这个:-
示例#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();
?>
movie[0]->characters->addChild('character');
$character->addChild('name','Mr.Parser');
$character->addChild('actor','johndoe');
$rating=$movies->movie[0]->addChild('rating','PG');
$rating->addAttribute('type','mpaa');
echo$movies->asXML();
?>
可能会有帮助。
<?xml version="1.0" encoding="utf-8" ?>
<Title0>
<Title1>Some Text 1</Title1>
</Title0>
<?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();
?>