dtxooq1020 2016-03-02 07:14
浏览 79

PHP调用未定义的方法DOMElement

I have wrap class for creating xml file and i dont know how to rewrite add() method to work correctly. Variable SHOPITEM should be parent element, but i dont know how to reach it in add method.

Any advice are welcome

  class Items {

    const XML_VERSION enter code here= '1.0';
    const XML_ENCODING = 'utf-8';
    const SHOP = 'SHOP';

    private $xml, $xmlElement;

    public function __construct() {
        $this->xml = new DOMDocument(self::XML_VERSION, self::XML_ENCODING);
        //$this->xml->preserveWhiteSpace = false;
        $this->xml->formatOutput = true;
        $this->xmlElement = $this->create(self::SHOP);
    }

    public function create($nodeName, $value = null) {
        return $this->xml->createElement($nodeName, $value);
    }

    public function add($object) {
        return $this->xmlElement->appendChild($object);
    }

    public function write() {
        $this->xml->appendChild($this->xmlElement);
        $this->xml->save("test.xml");
    }

}

$items = new Items();
$SHOPITEM = $items->create('SHOPITEM');
$product1 = $items->create('Product1', 'Some value 1');
$product2 = $items->create('Product2', 'Some value 2');

//this wont work
$SHOPITEM->add($product1);

//this works
$items->add($product1);
$items->add($product2);

//this works
$items->add($SHOPITEM);

$items->write();
  • 写回答

1条回答 默认 最新

  • dta43039 2016-03-02 08:21
    关注

    Your $SHOPITEM, $product1 and $product2 are instances of newly added node (i.e. of DOMElement class) because they are result of the statement

    return $this->xml->createElement($nodeName, $value);
    

    So they just do nat have a method add($object) that you want to use on them.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题