从表单数据导出XML

编程入门 行业动态 更新时间:2024-10-28 00:22:17
本文介绍了从表单数据导出XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试提出一个将表单数据导出为XML格式的常规例程。目的是使其足够通用,以使表单元素ID可以用作XML节点,并且表单值显然是节点内容。这是我到目前为止的内容:

I'm trying to come up with a general routine for exporting form data to an XML format. The goal would be to make it general enough that the form element IDs would serve as the XML nodes, and the form values would obviously be the node content. Here's what I have so far:

if(isset($_POST['submit'])) { $doc = new DOMDocument('1.0'); $doc->formatOutput = true; $post = $_POST; unset($post['submit']); $data = array_values($post); $headers = array_keys($post); /* TODO - wrap next two lines with if clause */ /* to check for existing root node */ $root = $doc->createElement('student'); $root = $doc->appendChild($root); for ($i = 0; count($headers) - 1; $i++) { /* Create and append node for each form element to root */ $theNode = $doc->createElement($headers[$i]); $theNode = $root->appendChild($theNode); /* Add content to node */ $theValue = $doc->createTextNode($data[$i]); $theValue = $theNode->appendChild($theValue); } $fh = fopen($file, 'w') or die("Can't open the XML file."); fwrite($fh, $doc->saveXML()); fclose($fh); header('Location: thanks.php'); }

并且,根据要求,这是表单代码,尽管我认为这无关紧要:

And, as requested, here's the form code, although I think that's irrelevant:

<form name="form1" method="post" action=""> <p> <label for="name">Name: </label> <input type="text" name="name" id="name" placeholder="Your full name" autofocus required> </p> <p> <label for="email">Email: </label> <input type="email" name="email" id="email"> </p> <p> <label for="cell">Cell: </label> <input type="tel" name="cell" id="cell"> </p> <p> <label for="dob">Date of birth: </label> <input type="date" name="dob" id="dob"> </p> <p> <label for="study">Years of art study: </label> 0 <input type="range" name="study" id="study" min="0" max="16"> 16 </p> <p style="text-align: center;"> <input type="submit" name="submit" id="submit" value="Submit"> </p> </form>

不幸的是,此代码导致HTTP错误500。我在做什么错吗?

Unfortunately, this code results with an HTTP Error 500. Any ideas on what I'm doing wrong?

哦,正如TODO所述,我还需要弄清楚文件中是否已经有数据(存在根节点),只需追加而不要重新插入根节点。我如何测试XML文件中的根节点?

Oh, and as the TODO notes, I need to also figure out if there already is data (a root node exists) in the file, just append and don't reinsert the root node. How do I test for a root node in an XML file?

非常感谢您的帮助-Joe

Thanks so much for your help - Joe

推荐答案

下面是一个代码片段,可以解决问题的第一部分。我添加了带有伪值的硬编码 $ _ POST 来模拟您的表单提交:

Here's a code snippet that'll solve the 1st part of your problem. I added a hard-coded $_POST with dummy values to simulate your form submission:

<?php $_POST = array( 'submit' => true, 'name' => 'Name', 'email' => 'Email', 'cell' => 'Cell', 'dob' => 'Date of birth', 'study' => 'Years of art study' ); if(isset($_POST['submit'])) { $doc = new DOMDocument('1.0'); $doc->formatOutput = true; $root = $doc->createElement('student'); $root = $doc->appendChild($root); $post = $_POST; unset($post['submit']); foreach ($post as $key => $value) { $node = $doc->createElement($key, $value); $root->appendChild($node); } echo $doc->saveXML(); }

输出:

Output:

<?xml version="1.0"?> <student> <name>Name</name> <email>Email</email> <cell>Cell</cell> <dob>Date of birth</dob> <study>Years of art study</study> </student>

对于问题的第二部分, DOMDocument :: saveXML 函数不会提示您下载文件或将文件保存到磁盘,而是根据其描述:

For the 2nd part of your problem, the DOMDocument::saveXML function doesn't prompt you to download a file or saves a file to disk, instead as its description states:

DOMDocument :: saveXML —将内部XML树转储回字符串

DOMDocument::saveXML — Dumps the internal XML tree back into a string

假设您希望能够下载生成的XML,请在SO中查看此问题的答案:如何使用php创建XML文件并提示下载? a>。

Assuming you want to be able to download the resulting XML take a look at the answer to this question here in SO: How do I create an XML file with php and have it prompt to download?.

更多推荐

从表单数据导出XML

本文发布于:2023-06-06 10:47:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/538995.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表单   数据   XML

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!