PHP和DOMDocument [英] PHP and DOMDocument
问题描述
我对DOMDocument的使用和创建XML有疑问。
我有一个PHP程序
- 加载到XML文件中
- 处理XML的每个节点(行);将其发送给另一个进程,然后该进程返回一个XML元素
- 我获得了该节点的字符串表示形式,以便可以创建(追加)到新的结果XML树中以返回到client
我遇到的问题是,在我尝试将最终文档返回给客户端之前,XML看起来还不错。我正在使用saveXML(),结果文件包含& lt& gt,等等。当我尝试保存到文件[save()]时,我也得到了这些结果。一直在PHP板上搜索数小时。
这是我的代码:
<?php
标头('Content-type:text / xml');
// ****载入XML ****
$ xml = simplexml_load_file(’Test1.xml’);
//实例化类;使用单实例
$ myAddress = new myAddressClass;
$ domDoc =新的DOMDocument(‘1.0’,‘UTF-8’);
$ domDoc-> formatOutput = true;
$ rootElt = $ domDoc-> createElement(’root’);
$ rootNode = $ domDoc-> appendChild($ rootElt);
//浏览XML的每一行并处理每个地址
foreach($ xml-> Row as $ row)
{
//触发功能针对实例
//返回SimpleXMLElement
$ resultXMLNode = $ myAddress-> buildRequest();
//需要节点
的XML表示$ subNode = $ addressXML-> asXML();
//删除无关的XML def
$ cleanSubNode = str_replace(’<?xml version = 1.0?>’,’,$ subNode);
//创建新节点
$ subElt = $ domDoc-> createElement(’MyResponse’,$ cleanSubNode);
//附加subElmt节点
$ rootNode-> appendChild($ subElt);
}
//需要格式正确/有效的完整XML文档/
$ domDoc-> saveXML();
?>
BTW,我将XML返回给客户端,以便随后可以通过jQuery生成HTML。 / p>
任何帮助将不胜感激。
此外,如果有人可以提供一种可能更有效的方法, ,那也很棒:)
谢谢。
Rob
要将XML(作为字符串)附加到另一个元素中,请创建一个文档片段,您可以在其后附加:
/ /创建新节点
$ subElt = $ domDoc-> createElement('MyResponse');
//创建新片段
$ fragment = $ domDoc-> createDocumentFragment();
$ fragment-> appendXML($ cleanSubNode);
$ subElt-> appendChild($ fragment);
这会将原始XML转换为domdocument元素,它使用了 DOMDocumentFragment :: appendXML
函数。
编辑:或者,在您的用例中(注释的thx),您可以直接使用simplexml对象和导入到您的domdocument中:
//创建子元素
$ subElt = $ domDoc-> createElement('MyResponse');
//导入simplexml文档
$ subElt-> appendChild($ domDoc-> importNode(dom_import_simplexml($ resultXMLNode),true));
//我们将新元素作为根(文档的子级)插入
$ domDoc-> appendChild($ subElt);
无需将响应转换为字符串并使用以下方法进行替换操作这个。
I have a question regarding the use of the DOMDocument and creating XML.
I have a PHP program that
- loads in an XML file
- processes each node (row) of XML; sends it off to another process which then returns an XML element
- I get the string representation of the node so that I can create (append) to a new, resultant XML tree for return to the client
The problem I have is, the XML looks fine until I try and return the final doc back to client. I am using saveXML() and the resultant file contains < >, etc. When I try to do a save to file [save()] I also get those results. Been searching the PHP boards for hours on this.
Here's my code:
<?php
header('Content-type: text/xml');
// **** Load XML ****
$xml = simplexml_load_file('Test1.xml');
// Instantiate class; work with single instance
$myAddress = new myAddressClass;
$domDoc = new DOMDocument('1.0', 'UTF-8');
$domDoc->formatOutput = true;
$rootElt = $domDoc->createElement('root');
$rootNode = $domDoc->appendChild($rootElt);
//Go through each row of XML and process each address
foreach($xml->Row as $row)
{
//fire off function against instance
// returns SimpleXMLElement
$resultXMLNode = $myAddress->buildRequest() ;
// need XML representation of node
$subNode = $addressXML->asXML();
// strip out extraneous XML def
$cleanSubNode = str_replace('<?xml version="1.0"?>', '', $subNode);
// create new node
$subElt = $domDoc->createElement('MyResponse', $cleanSubNode );
//append subElmt node
$rootNode->appendChild($subElt);
}
// need full XML doc properly formatted/valid
$domDoc->saveXML();
?>
BTW, I am returning the XML to the client so that I can then produce HTML via jQuery.
Any help would be appreciated.
Also, if anyone can offer up a potentially more efficient way of doing this, that'd be great too :)
Thanks.
Rob
To append XML (as a string) into another element, you create a document fragment which you can append then:
// create new node
$subElt = $domDoc->createElement('MyResponse');
// create new fragment
$fragment = $domDoc->createDocumentFragment();
$fragment->appendXML($cleanSubNode);
$subElt->appendChild($fragment);
This will convert the raw XML into domdocument elements, it's making use of the DOMDocumentFragment::appendXML
function.
Edit: Alternatively in your use-case (thx to the comments), you can directly use the simplexml object and import it into your domdocument:
// create subelement
$subElt = $domDoc->createElement('MyResponse');
// import simplexml document
$subElt->appendChild($domDoc->importNode(dom_import_simplexml($resultXMLNode), true));
// We insert the new element as root (child of the document)
$domDoc->appendChild($subElt);
No need to convert the response into a string and do the replace operation you do with it any longer with this.
这篇关于PHP和DOMDocument的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!