douxin2003 2013-11-19 11:14
浏览 34
已采纳

如何在元素标记之间创建包含文本的元素

I have made this function:

protected function _addElement($xmlDocument, $elementName, $parentElement){
    $element = $xmlDocument->createElement($elementName);
    return $parentElement->appendChild($element);
}

and I call it, i.e., in this way:

$Street = $this->_addElement($xmlDocument, 'Steet', $xmlDocument);
$House = $this->_addElement($xmlDocument, 'House', $Street);

and in the created XML document I will have it looking like that:

<Street>
   <House //maybe some attributes />
</Street>

So, the createElement($elementName) function is from the DOM API, that I call upon the DOM object $xmlDocument and it will create a closed node. What I need is to use a function that, with which I will get to the following result:

<Street>
   <Yard> the dog pooped all over the backyard </Yard>
</Steet>

I looked in the DOM API for something involving text, etc. I stumbled upon createTextNode($content) but that is something kinda' different, or maybe I just didn't understand correctly, when I read the documentiation.

  • 写回答

1条回答 默认 最新

  • duanhaodi4809 2013-11-19 11:26
    关注

    Since you're using DOMDocument::createElement, you'll notice from the method definition that it has a 2nd non-mandatory argument:

    public DOMElement DOMDocument::createElement ( string $name [, string $value ] )
    

    Just update your code to:

    protected function _addElement($xmlDocument, $elementName, $elementValue, $parentElement) {
        $element = $xmlDocument->createElement($elementName, $elementValue);
        return $parentElement->appendChild($element);
    }
    

    and:

    $Street = $this->_addElement($xmlDocument, 'Steet', 'My street', $xmlDocument);
    $House  = $this->_addElement($xmlDocument, 'House', 'My house', $Street);
    

    That way you'll be able to add elements with the values you want. Remember that if you want to add attributes to the newly created elements you'll have to use the DOMDocument::createAttribute function on the $element variable before calling appendChild.

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

报告相同问题?

悬赏问题

  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。