php 使用 SimpleXML 从头开始​​创建 XML 对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/143122/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 21:39:31  来源:igfitidea点击:

Using SimpleXML to create an XML object from scratch

phpxmlsimplexml

提问by dirtside

Is it possible to use PHP's SimpleXML functions to create an XML object from scratch? Looking through the function list, there's ways to import an existing XML string into an object that you can then manipulate, but if I just want to generate an XML object programmatically from scratch, what's the best way to do that?

是否可以使用 PHP 的 SimpleXML 函数从头开始创建 XML 对象?查看函数列表,有多种方法可以将现有的 XML 字符串导入到可以操作的对象中,但是如果我只想从头开始以编程方式生成 XML 对象,那么最好的方法是什么?

I figured out that you can use simplexml_load_string() and pass in the root string that you want, and then you've got an object you can manipulate by adding children... although this seems like kind of a hack, since I have to actually hardcode some XML into the string before it can be loaded.

我发现你可以使用 simplexml_load_string() 并传入你想要的根字符串,然后你就有了一个可以通过添加孩子来操作的对象......虽然这看起来有点像黑客,因为我必须实际上在加载之前将一些 XML 硬编码到字符串中。

I've done it using the DOMDocument functions, although it's a little confusing because I'm not sure what the DOM has to do with creating a pure XML document... so maybe it's just badly named :-)

我已经使用DOMDocument 函数完成了它,虽然它有点令人困惑,因为我不确定 DOM 与创建纯 XML 文档有什么关系......所以也许它只是命名不好:-)

回答by DreamWerx

Sure you can. Eg.

你当然可以。例如。

<?php
$newsXML = new SimpleXMLElement("<news></news>");
$newsXML->addAttribute('newsPagePrefix', 'value goes here');
$newsIntro = $newsXML->addChild('content');
$newsIntro->addAttribute('type', 'latest');
Header('Content-type: text/xml');
echo $newsXML->asXML();
?>

Output

输出

<?xml version="1.0"?>
<news newsPagePrefix="value goes here">
    <content type="latest"/>
</news>

Have fun.

玩得开心。

回答by PhiLho

In PHP5, you should use the Document Object Modelclass instead. Example:

在 PHP5 中,您应该改用Document Object Model类。例子:

$domDoc = new DOMDocument;
$rootElt = $domDoc->createElement('root');
$rootNode = $domDoc->appendChild($rootElt);

$subElt = $domDoc->createElement('foo');
$attr = $domDoc->createAttribute('ah');
$attrVal = $domDoc->createTextNode('OK');
$attr->appendChild($attrVal);
$subElt->appendChild($attr);
$subNode = $rootNode->appendChild($subElt);

$textNode = $domDoc->createTextNode('Wow, it works!');
$subNode->appendChild($textNode);

echo htmlentities($domDoc->saveXML());

回答by Stefan Gehrig

Please see my answer here. As dreamwerx.myopenid.compoints out, it is possible to do this with SimpleXML, but the DOM extensionwould be the better and more flexible way. Additionally there is a third way: using XMLWriter. It's much more simple to use than the DOM and therefore it's my preferred way of writing XML documents from scratch.

在此处查看我的回答。正如dreamwerx.myopenid.com指出的那样,使用SimpleXML可以做到这一点,但DOM 扩展将是更好、更灵活的方式。另外还有第三种方法:使用XMLWriter。它比 DOM 更易于使用,因此它是我从头开始编写 XML 文档的首选方式。

$w=new XMLWriter();
$w->openMemory();
$w->startDocument('1.0','UTF-8');
$w->startElement("root");
    $w->writeAttribute("ah", "OK");
    $w->text('Wow, it works!');
$w->endElement();
echo htmlentities($w->outputMemory(true));

By the way: DOMstands for Document Object Model; this is the standardized API into XML documents.

顺便说一句:DOM代表document Øbject中号奥德尔; 这是将标准化的 API 转化为 XML 文档。