【问题标题】:Can't create xml correctly using DOM PHP无法使用 DOM PHP 正确创建 xml
【发布时间】:2013-11-25 21:29:34
【问题描述】:

我正在尝试使用 php 从 html 表单创建这样的 XML。我想创建复杂的元素

<recipe>
<portion_size>
<portion_num>
</portion_num>
</portion_size>

<recipe_description>
<prep_time>
</prep_time
<recipe descrip>
</recipe descrip>
<recipe_description>

类似的东西,但是我似乎无法使用 DOM 正确嵌套它,这是我的 php 代码。有人有什么建议吗?

$doc = new DOMDocument(); //open the object xml 
    $r = $doc->createElement("recipe"); 
    $doc->appendChild($r); 

    $name = $doc->createElement('name',$name);
    $r->appendChild($name); 

    $r = $doc->createElement("portion_size");
    $doc->appendChild($r);

    $child = $doc->createElement('portion_num',$portionNum);
    $r->appendchild($child);

     $prepTime = $doc->createElement('prep_time',$prepTime);
    $r->appendChild($prepTime);

     $recipeDescrip = $doc->createElement('descrip',$recipeDescrip);
    $r->appendChild($recipeDescrip);

     $utensilNum = $doc->createElement('utensil_num',$utensilNum);
    $r->appendChild($utensilNum);

     $utensilDescrip = $doc->createElement('utensil_descrip',$utensilDescrip);
    $r->appendChild($utensilDescrip);

     $quantityAmount = $doc->createElement('quantity_amount',$quantityAmount);
    $r->appendChild($quantityAmount); 

     $ingredientName = $doc->createElement('ingredient_name',$ingredientName);
    $r->appendChild($ingredientName); 

     $stepNum = $doc->createElement('step_num',$stepNum);
    $r->appendChild($stepNum); 

     $stepDetail = $doc->createElement('step_detail',$stepDetail);
    $r->appendChild($stepDetail); 

【问题讨论】:

  • 我认为它适用于我的份量大小,但是它不会在 part_num 之后关闭它,而是在 xml 末尾关闭它
  • 只用 DOM 来创建 xml 是“正确的方法”,但它也非常痛苦。您最好将其全部构建为一个巨大的字符串 $xml = "&lt;recipe&gt;&lt;foo&gt;&lt;bar /&gt;&lt;/foo&gt;&lt;/recipe"; 并继续处理更重要的事情。
  • 这可能会导致更多的工作。您必须注意转义并确保 xml 格式正确。

标签: javascript php xml


【解决方案1】:

您不应该使用 DOMDocument::createElement() 的第二个参数,它不是 w3c dom api 的一部分,并且似乎已损坏。您需要使用 DOMDocument::createTextNode()。不过这会有点吵,所以我建议你把这个工作封装到一个方法中。

class MyDOMElement extends DOMElement {

  public function appendElement($name, array $attributes = NULL, $content = '') {
    $node = $this->ownerDocument->createElement($name);
    if (!empty($attributes)) {
      foreach ($attributes as $key => $value) {
        $node->setAttribute($key, $value);
      }
    }
    if (!empty($content)) {
      $node->appendChild($this->ownerDocument->createTextNode($content));
    }
    $this->appendChild($node);
    return $node;
  }
}

class MyDOMDocument extends DOMDocument {

  public function __construct($version = '1.0', $encoding= 'utf-8') {
    parent::__construct($version, $encoding);
    $this->registerNodeClass('DOMElement', 'MyDOMElement');
  }
}

$dom = new MyDOMDocument();
$dom->appendChild($root = $dom->createElement('recipe'));

$root->appendElement('name', NULL, 'Reciepe Example');
$root
  ->appendElement('portion_size')
  ->appendElement('portion_num', NULL, '4');
$description = $root->appendElement('recipe_description');
$description->appendElement('prep_time', array('unit' => 'minutes'), '10');
//...

echo $dom->saveXml();

输出:

<?xml version="1.0" encoding="utf-8"?>
<recipe>
  <name>Reciepe Example</name>
  <portion_size>
    <portion_num>4</portion_num>
  </portion_size>
  <recipe_description>
    <prep_time unit="minutes">10</prep_time>
  </recipe_description>
</recipe>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-19
    • 2012-10-30
    • 1970-01-01
    • 2014-10-25
    • 2015-06-04
    相关资源
    最近更新 更多