如何使用symfony 3.4将XML soap请求转换为DOMDocument

时间:2018-11-08 08:40:38

标签: xml symfony dom soap

我正在尝试将xml代码转换为Dom 我成功创建了肥皂创建,更新,删除请求,它一切正常,问题是xml代码的可读性有点困难,现在我决定将xml转换为dom

创建操作:

/**
 *  Returns the soap create body for the Newsletter Entity.
 * 
 * @param Newsletter $newsletter
 * @param type $action
 * @return string 
 */
public function getSoapCreateNewsletterBody(Newsletter $newsletter) {

    $soapBody = '
     <Create xmlns="http://schemas.microsoft.com/xrm/2011/Contracts/Services">
                <entity xmlns:b="http://schemas.microsoft.com/xrm/2011/Contracts" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
                    <b:Attributes xmlns:c="http://schemas.datacontract.org/2004/07/System.Collections.Generic">' .
            $this->getFieldXml(self::STRING_TYPE, $newsletter->getEmail(), 'new_email') .
            $this->getFieldXml(self::STRING_TYPE, $newsletter->getName(), 'new_name') .
            '</b:Attributes>
                    <b:EntityState i:nil="true"/>
                    <b:FormattedValues xmlns:c="http://schemas.datacontract.org/2004/07/System.Collections.Generic"/>
                    <b:Id>00000000-0000-0000-0000-000000000000</b:Id>
                    <b:LogicalName>new_newsletter</b:LogicalName>
                    <b:RelatedEntities xmlns:c="http://schemas.datacontract.org/2004/07/System.Collections.Generic"/>
                </entity>
                </Create>';

    return $soapBody;
}

删除动作:

 /**
 * Returns the soap delete body for th Newsletter Entity
 * 
 * @param Newsletter $newsletter
 * @return string
 */
public function getSoapDeleteNewsletterBody(Newsletter $newsletter){

       $soapBody = '
              <Delete xmlns="http://schemas.microsoft.com/xrm/2011/Contracts/Services">
                 <entityName>new_newsletter</entityName>
                 <id>' . $newsletter->getNewsletteridcrm() . '</id>                      
              </Delete>';

    return $soapBody;
} 

这里我正在尝试将xml转换为dom,出现以下代码

/**
 * Returns the soap delete body for th Newsletter Entity
 * 
 * @param Newsletter $newsletter
 * @return string
 */
public function getSoapDeleteNewsletterBody(Newsletter $newsletter){

    /* Generate the DeleteRequest message */
    $deleteRequestDOM = new DOMDocument();
    $deleteNode = $deleteRequestDOM->appendChild( $deleteRequestDOM->createElementNS( 'http://schemas.microsoft.com/xrm/2011/Contracts/Services', 'Delete' ) );       
    $deleteNode->appendChild( $deleteRequestDOM->createElement( 'new_newsletter', $newsletter ) );
    $deleteNode->appendChild( $deleteRequestDOM->createElement( 'id', $newsletter->getNewsletteridcrm()->ID ) );
    /* Return the DOMNode */
    return $deleteNode;     
} 

谁能告诉我我该怎么做... 预先感谢...

更新的工作代码:

/**
 * Returns the soap delete body for th Newsletter Entity
 * 
 * @param Newsletter $newsletter
 * @return string
 */
public function getSoapDeleteNewsletterBody(Newsletter $newsletter){

    /* Generate the DeleteRequest message */
    $deleteRequestDOM = new DOMDocument();
    $deleteNode = $deleteRequestDOM->appendChild( $deleteRequestDOM->createElementNS( 'http://schemas.microsoft.com/xrm/2011/Contracts/Services', 'Delete' ) );       
    $deleteNode->appendChild( $deleteRequestDOM->createElement( 'entityName', 'new_newsletter' ) );

    $deleteNode->appendChild( $deleteRequestDOM->createElement( 'id', $newsletter->getNewsletteridcrm()) );
    /* Return the DOMNode */

    return $deleteRequestDOM->saveXML($deleteRequestDOM->documentElement);
} 

1 个答案:

答案 0 :(得分:0)

您的问题是由以下行引起的:

$deleteRequestDOM->createElement( 'new_newsletter', $newsletter )

此处$newsletter是一个Newsletter对象,但是createElement期望一个字符串作为第二个参数(XML元素的值)。看一下函数的documentation

您真正想要的是...

$deleteRequestDOM->createElement('entityName', 'new_newsletter')

...创建此XML节点:

<entityName>new_newsletter</entityName>