php - 使用 DOMDocument 类创建 xml 消息的最佳实践

标签 php xml class dom

我想为网络服务通信创建 xml 消息。 这些消息应该从可重用元素池中创建。 因此我创建了不同的类。 一个“工厂”类,只返回一个消息类。 一个元素类,由可重用部分和消息类组成,消息类是所需 xml 消息的蓝图。

我的代码提供了预期的结果,但我正在寻找最佳实践。 尤其是摆脱在每个消息类中重写相同的 save() 和 *__construct* 方法的方法。

提前致谢

    // class to create webservice messages
class Messages{

    private function __construct(){}

    public static function get($type) {
        //some error handling if class not exists
        return new $type;
    }
}

// message no.1
class Message_1 extends Elements{

    protected $root;

    public function __construct() {
        parent::__construct();
        $this->root = $this->createElement("message1");
    }

    public function add_anotherElement(){
        $this->root->appendChild($this->add_anotherElementBlock("foo", "bar"));
    }

    public function add_element(){
        $this->root->appendChild($this->add_someElementBlock("foo", "bar"));
    }

    public function save(){
        return $this->saveXML($this->root);
    }
}

// message no.2
class Message_2 extends Elements {

    protected $root;

    public function __construct() {
        parent::__construct();
        $this->root = $this->createElement("message2");
    }

    public function add_elements(){
        $this->root->appendChild($this->add_anotherElementBlock("foo", "bar"));
        $this->root->appendChild($this->add_someElementBlock("foo", "bar"));
    }

    public function save(){
        return $this->saveXML($this->root);
    }
}

// reusable elements
class Elements extends DOMDocument{

    public function __construct() {
        parent::__construct();
    }

    public function add_someElementBlock($foo, $bar) {
        $node = $this->createElement("root");

        $attr = $this->createAttribute("id");
        $attr->value = $foo;
        $node->appendChild($attr);

        $subnode = $this->createElement("sub",$bar);

        $node->appendChild($subnode);

        return $node;
    }

    public function add_anotherElementBlock($foo, $bar) {
        $node = $this->createElement("anotherRoot");

        $subnode = $this->createElement("anotherSubNode",$bar);
        $attr = $this->createAttribute("anotherAttribute");
        $attr->value = $foo;
        $subnode->appendChild($attr);

        $node->appendChild($subnode);

        return $node;
    }
}

$message1 = Messages::get('Message_1');
$message1->add_element();
$message1->add_anotherElement();


$message2 = Messages::get('Message_2');
$message2->add_elements();

//********************************************
echo "<pre>";
print_r(htmlentities($message1->save()));
echo "</pre>";
echo "<hr />";
echo "<pre>";
print_r(htmlentities($message2->save()));
echo "</pre>";

最佳答案

感谢 hek2mgl 的建议,我改变了我的类(class)。对我来说似乎不错,希望对任何人也有帮助。

// class to create webservice messages
class Messages{

    private function __construct(){}

    public static function get($type) {
        //some error handling if class not exists
        return new $type;
    }
}

// message no.1
class Message_1 extends Elements{

    public function __construct() {
        parent::__construct();
        $this->root = $this->createElement("message1");
    }

    public function add_anotherElement(){
        $this->root->appendChild($this->add_anotherElementBlock("foo", "bar"));
    }

    public function add_element(){
        $this->root->appendChild($this->add_someElementBlock("foo", "bar"));
    }
}

// message no.2
class Message_2 extends Elements {

    public function __construct() {
        parent::__construct();
        $this->root = $this->createElement("message2");
    }

    public function add_elements(){
        $this->root->appendChild($this->add_anotherElementBlock("foo", "bar"));
        $this->root->appendChild($this->add_someElementBlock("foo", "bar"));
    }

}

// message no.3
class Message_3 extends Elements {

    public function __construct() {
        parent::__construct();
        $this->root = $this->createElement("message3");
    }

    public function add_element(){
        // unique Element
        $this->root->appendChild($this->createElement("foo", "bar"));
    }

}

// reusable elements
class Elements extends DOMDocument{

    protected $root;

    public function __construct() {
    }

    protected function add_someElementBlock($foo, $bar) {
        $node = $this->createElement("root");

        $attr = $this->createAttribute("id");
        $attr->value = $foo;
        $node->appendChild($attr);

        $subnode = $this->createElement("sub",$bar);

        $node->appendChild($subnode);

        return $node;
    }

    protected function add_anotherElementBlock($foo, $bar) {
        $node = $this->createElement("anotherRoot");

        $subnode = $this->createElement("anotherSubNode",$bar);
        $attr = $this->createAttribute("anotherAttribute");
        $attr->value = $foo;
        $subnode->appendChild($attr);

        $node->appendChild($subnode);

        return $node;
    }

    public function getMessage(){
        return $this->saveXML($this->root);
    }
}

$message1 = Messages::get('Message_1');
$message1->add_element();
$message1->add_anotherElement();

$message2 = Messages::get('Message_2');
$message2->add_elements();

$message3 = Messages::get('Message_3');
$message3->add_element();

//********************************************
echo "<pre>";
print_r(htmlentities($message1->getMessage()));
echo "</pre>";
echo "<hr />";
echo "<pre>";
print_r(htmlentities($message2->getMessage()));
echo "</pre>";
echo "<hr />";
echo "<pre>";
print_r(htmlentities($message3->getMessage()));
echo "</pre>";

关于php - 使用 DOMDocument 类创建 xml 消息的最佳实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21913809/

相关文章:

php - MySQL按不同日期选择,有日期限制但没有条目

java - 从 Android 中的 FragmentActivity 类访问 Fragment 类方法

Java - 如何将从一个类的方法中生成的变量传递到另一个类?

php - .htaccess 重写子目录不起作用

javascript - 无法使用 SDK v5 检索 facebook cookie

java - 删除 xml 版本控制 + 启动节点

python - 如何将xml header 添加到dom对象

java - 如何将类实现到 ActionListener 中?

c++ - 类内的动态数组

php - 带有 Nginx 1.6.2 和 PHP 5.6.4 的 Ubuntu 14.04 上的 502 Bad Gateway