Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在SimpleXMLElement中编写原始xml_Php - Fatal编程技术网

Php 在SimpleXMLElement中编写原始xml

Php 在SimpleXMLElement中编写原始xml,php,Php,我制作了一个simplexmlement,我想在它里面写我自己的XML,每当我写XML时,它会返回和其他转义字符。使用XMLWriter的writeRaw函数,我以前可以编写,但是当我使用它时,答案会返回一个错误,而且我似乎无法修复它。SimpleXML不直接具有此功能,但它在函数族中可用。谢天谢地,在同一个XML文档上同时使用SimpleXML和DOM很容易 <?php $example = new SimpleXMLElement('<example/>'); // &

我制作了一个
simplexmlement
,我想在它里面写我自己的XML,每当我写XML时,它会返回
和其他转义字符。使用
XMLWriter
writeRaw
函数,我以前可以编写
,但是当我使用它时,答案会返回一个错误,而且我似乎无法修复它。

SimpleXML不直接具有此功能,但它在函数族中可用。谢天谢地,在同一个XML文档上同时使用SimpleXML和DOM很容易

<?php

$example = new SimpleXMLElement('<example/>');

// <example/> as a DOMElement
$dom = dom_import_simplexml($example);

// Create a new DOM document fragment and put it inside <example/>
$fragment = $dom->ownerDocument->createDocumentFragment();
$fragment->appendXML('<a>a</a><b>b</b>');
$dom->appendChild($fragment);

// Back to SimpleXML, it can see our document changes
echo $example->asXML();

?>
下面的示例使用文档片段向文档添加两个元素

<?php

$example = new SimpleXMLElement('<example/>');

// <example/> as a DOMElement
$dom = dom_import_simplexml($example);

// Create a new DOM document fragment and put it inside <example/>
$fragment = $dom->ownerDocument->createDocumentFragment();
$fragment->appendXML('<a>a</a><b>b</b>');
$dom->appendChild($fragment);

// Back to SimpleXML, it can see our document changes
echo $example->asXML();

?>
ownerDocument->createDocumentFragment();
$fragment->appendXML('ab');
$dom->appendChild($fragment);
//回到SimpleXML,它可以看到我们的文档更改
echo$example->asXML();
?>
上述示例输出:

<?xml version="1.0"?>
<example><a>a</a><b>b</b></example>

ab

删除我的答案可能重复,因为问题重复。你应该考虑删除这个问题。