douguanya6399
2016-11-19 07:01
浏览 47

使用DOMDocument()在php中创建XML文件

I want to create XML file in php and I have saved the values from array to variable

<?php
$name = $e['name_1'];
$email = $e['email_id'];
$phone_no =$e['phone_no'];

$doc = new DOMDocument();
$doc->formatOutput = true;

$ele1 = $doc->createElement('StudentName');
$ele1->nodeValue=$name;
$doc->appendChild($ele1);

$ele2 = $doc->createElement('FatherEmailId');
$ele2->nodeValue=$email;
$doc->appendChild($ele2);

$ele3 = $doc->createElement('PhoneNumber');
$ele3->nodeValue=$phone_no;
$doc->appendChild($ele3);

$doc->save('MyXmlFile007.xml');  

?>

i want my XML formatted lik this

<?xml version="1.0"?>
<StudentDetails>
<StudentName>Pravin Parayan</StudentName>
<FatherEmailId>pravinp@pigtailpundits.com</FatherEmailId>
<PhoneNumber>9000012345</PhoneNumber>
<StudentDetails/>

But instead of the above i get something lik this

<?xml version="1.0"?>
<StudentDetails/>
<StudentName>Joel George</StudentName>
<FatherEmailId>joy@pigtailpundits.com</FatherEmailId>
<PhoneNumber>9000012345</PhoneNumber>

图片转代码服务由CSDN问答提供 功能建议

我想在php中创建XML文件,我已将值从数组保存到变量 <前> &lt;?php $ name = $ e ['name_1']; $ email = $ e ['email_id']; $ phone_no = $ e ['phone_no'] ; $ doc = new DOMDocument(); $ doc-&gt; formatOutput = true; $ ele1 = $ doc-&gt; createElement('StudentName'); $ ele1-&gt; nodeValue = $ name; $ doc-&gt; appendChild($ ele1); $ ele2 = $ doc-&gt; createElement('FatherEmailId'); $ ele2-&gt; nodeValue = $ email; \ n $ doc-&gt; appendChild($ ele2); $ ele3 = $ doc-&gt; createElement('PhoneNumber'); $ ele3-&gt; nodeValue = $ phone_no; $ doc-&gt; 的appendChild($ ele3); $的doc-&GT;保存( 'MyXmlFile007.xml'); ?&gt;

我希望我的XML格式化这个

 &lt;?xml版本 =“1.0”?&gt; 
&lt; StudentDetails&gt; 
&lt; StudentName&gt; Pravin Parayan&lt; / StudentName&gt; 
&lt; FatherEmailId&gt; pravinp@pigtailpundits.com< / FatherEmailId&gt; 
&lt; PhoneNumber&gt; 9000012345&lt; / PhoneNumber&gt; 
&lt;  StudentDetails /&gt; 
   
 
 

但不是上面的内容我得到了这个

 &lt;?xml版本 =“1.0”?&gt; 
&lt; StudentDetails /&gt; 
&lt; StudentName&gt; Joel George&lt; / StudentName&gt; 
&lt; FatherEmailId&gt; joy@pigtailpundits.com< / FatherEmailId&gt; 
&lt; PhoneNumber&gt; 9000012345&lt; / PhoneNumber&gt; \  n   
 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongzouqie4220 2016-11-19 09:01
    已采纳

    All you need to do is to add a root element StudentDetails, and append all other elements ti it, like the following:

    <?php
    $name = $e['name_1'];
    $email = $e['email_id'];
    $phone_no =$e['phone_no'];
    
    $doc = new DOMDocument();
    $doc->formatOutput = true;
    
    $root = $doc->createElement('StudentDetails');
    $root = $doc->appendChild($root);
    
    $ele1 = $doc->createElement('StudentName');
    $ele1->nodeValue=$name;
    $root->appendChild($ele1);
    
    $ele2 = $doc->createElement('FatherEmailId');
    $ele2->nodeValue=$email;
    $root->appendChild($ele2);
    
    $ele3 = $doc->createElement('PhoneNumber');
    $ele3->nodeValue=$phone_no;
    $root->appendChild($ele3);
    
    $doc->save('MyXmlFile007.xml');
    

    And the result would be like:

    <?xml version="1.0"?>
    <StudentDetails>
      <StudentName>Pravin Parayan</StudentName>
      <FatherEmailId>pravinp@pigtailpundits.com</FatherEmailId>
      <PhoneNumber>9000012345</PhoneNumber>
    </StudentDetails>
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题