doouzlrvb01417498 2014-12-11 09:30
浏览 31
已采纳

使用PHP将文件附加到DomDocument

This is the XML structure that I need to re-build with DomDocument (PHP):

<c:calendar-query xmlns:d="DAV:" xmlns:c="urn:ietf:params:xml:ns:caldav">
    <d:prop>
        <d:getetag />
        <c:calendar-data />
    </d:prop>
    <c:filter>
        <c:comp-filter name="VCALENDAR" />
    </c:filter>
</c:calendar-query>

This is my code:

$doc  = new DOMDocument('1.0', 'utf-8');
$doc->formatOutput = true;

$query = $doc->createElement('c:calendar-query');
$query->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:c', 'urn:ietf:params:xml:ns:caldav');
$query->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:d', 'DAV:');

$prop = $doc->createElement('d:prop');
$prop->appendChild($doc->createElement('d:getetag'));
$prop->appendChild($doc->createElement('c:calendar-data'));

$filter = $doc->createElement('c:filter');
$filter->appendChild($doc->createElement('c:comp-filter'));

$query->appendChild($prop);
$query->appendChild($filter);
$doc->appendChild($query);
$body = $doc->saveXML();

How do I add <c:comp-filter name="VCALENDAR" /> to the DOM?

  • 写回答

1条回答 默认 最新

  • dougou8573 2014-12-11 13:51
    关注

    You've already got a <c:comp-filter/>, just use setAttribute() to give it the attribute.


    Example:

    $doc  = new DOMDocument('1.0', 'utf-8');
    $doc->formatOutput = true;
    
    $query = $doc->createElement('c:calendar-query');
    $query->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:c', 'urn:ietf:params:xml:ns:caldav');
    $query->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:d', 'DAV:');
    
    $prop = $doc->createElement('d:prop');
    $prop->appendChild($doc->createElement('d:getetag'));
    $prop->appendChild($doc->createElement('c:calendar-data'));
    
    $filter = $doc->createElement('c:filter');
    $comp_filter = $doc->createElement('c:comp-filter');
    $comp_filter->setAttribute('name', 'VCALENDAR');
    $filter->appendChild($comp_filter);
    
    $query->appendChild($prop);
    $query->appendChild($filter);
    $doc->appendChild($query);
    $body = $doc->saveXML();
    
    echo $body;
    

    Output:

    <c:calendar-query xmlns:c="urn:ietf:params:xml:ns:caldav" xmlns:d="DAV:">
      <d:prop>
        <d:getetag/>
        <c:calendar-data/>
      </d:prop>
      <c:filter>
        <c:comp-filter name="VCALENDAR"/>
      </c:filter>
    </c:calendar-query>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?