duanmu3049 2011-11-05 06:45
浏览 39
已采纳

PHP和DOMDocument

I have a question regarding the use of the DOMDocument and creating XML.

I have a PHP program that

  1. loads in an XML file
  2. processes each node (row) of XML; sends it off to another process which then returns an XML element
  3. 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 &lt &gt, 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

  • 写回答

1条回答 默认 最新

  • duanjuan3931 2011-11-05 08:29
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度