0

我正在尝试生成一个 xml 文件:-

    <?php
    // create domtype
    $dom = new DOMDocument("1.0");
    // display document in browser as plain text 
    // for readability purposes
    header("Content-Type: text/plain");
    // create root element
    $root = $dom->createElement("products");
    $dom->appendChild($root);
    //create product_id
    $pro_id = $dom->createElement("product_id");
    $root -> appendChild($pro_id);
    //append attribute ib product_id
    $attpro = $dom->createAttribute("value");
    $pro_id -> appendChild($attpro);
    //append attribue value in product_id
    $attval = $dom->createTextNode('1');
    $attpro -> appendChild($attval);
    //create tab_id
    $tab_id = $dom->createElement("tab_id");
    $pro_id-> appendChild($tab_id);
    //create attibute value
    $atttab_id = $dom->createAttribute("value");
    $tab_id -> appendChild($atttab_id);
    //append attribute value in tab_id
    $attval2 = $dom->createTextNode('351');
    $atttab_id -> appendChild($attval2);
    file_put_contents("$a.txt",$dom->saveXML());
    echo $dom->saveXML();
    ?>

这是生成这种类型的 xml 文件:-

a.xml

<products>
  <product_id value='1'>
     <tab_id value='351' />
  </product_id>
</products> 

但我想要这种类型的输出: -

a.xml

<products>
  <product_id value='1'>
     <tab_id value='351' />
  </product_id>
</products> 


b.xml

<products>
  <tab_id value='351' />
</products>
4

0 回答 0