dougou5852 2012-12-05 07:30
浏览 19

PHP / XML / XSL的问题

I am trying to figure out what I can do to create a code that appends data in my XML file not rewrite the XML file continuously.

I need to be able to save all the form entries and as of right now every time the form is submitted. it creates a new XML file and erases the old one.

This may be really easy to fix or I am just really dumb but I have looked at DOM syntax and do not see what I could change to change the outcome.

// define configuration file name and path
$configFile = 'abook.xml';

// if form not yet submitted
// display form
if (!isset($_POST['submit'])) {

  // set up array with default parameters
  $data = array();
  $data['name'] = null;
  $data['email'] = null;
  $data['caddress'] = null;
  $data['city'] = null;
  $data['state'] = null;
  $data['zipcode'] = null;
  $data['phone'] = null;
  $data['pug'] = null;
  $data['comment'] = null;
  $data['subscribe'] = null;

  // read current configuration values
  // use them to pre-fill the form
  if (file_exists($configFile)) {
    $doc = new DOMDocument();
    $doc->preserveWhiteSpace = false;
    $doc->load($configFile);
    $address = $doc->getElementsByTagName('address');
    foreach ($address->item(0)->childNodes as $node) {
      $data[$node->nodeName] = $node->nodeValue; 

    }        
  }

In between is a PHP form and validation code and at the end I use XML tags again:

  // generate new XML document
  $doc = new DOMDocument();

  // create and attach root element <configuration> 
  $root = $doc->createElement('addressbook');
  $configuration = $doc->appendChild($root);

  // create and attach <oven> element under <configuration>
  $address = $doc->createElement('address');
  $configuration->appendChild($address);

  // write each configuration value to the file
  foreach ($config as $key => $value) {
    if (trim($value) != '') {
      $elem = $doc->createElement($key);
      $text = $doc->createTextNode($value);
      $address->appendChild($elem);
      $elem->appendChild($text);          
    }
  }

  // format XML output



  // save XML file
  $doc->formatOutput = true;
  $doc->save($configFile) or die('ERROR: Cannot write configuration file');      
  echo 'Thank you for filling out an application.';
}  

I am really new at this so I am sorry if my code is pretty messy.

The second part I am dealing with an XSL file which I have linked to my XML file but no matter what syntax I have used to transform, nothing works to save it in a table.

Again, I don't know if this could be caused by the way I have set up my PHP to write the XML.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 运动想象脑电信号数据集.vhdr
    • ¥15 三因素重复测量数据R语句编写,不存在交互作用
    • ¥15 微信会员卡等级和折扣规则
    • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
    • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
    • ¥15 gdf格式的脑电数据如何处理matlab
    • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
    • ¥100 监控抖音用户作品更新可以微信公众号提醒
    • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
    • ¥70 2048小游戏毕设项目