0

I have a problem while creating an xml file in php.I am not getting th root element.But all other elements are been outputted in the result.What might be the reason????Any help would be appreciated.The code is depicted below.order is my root element.

<?php
$url='success.xml';
//$xml = simplexml_load_file($url);


$name = htmlentities($_POST);
$age = htmlentities($_POST);

$doc = new DOMDocument();
$root=$doc->createElement("order");
$doc->appendChild($root);
$textareaNode = $doc->createElement("name");
$textNode = $doc->createTextNode($name);
$textareaNode->appendChild($textNode);
$doc->appendChild($textareaNode);
$textareaNode = $doc->createElement("age");
$textNode = $doc->createTextNode($age);
$textareaNode->appendChild($textNode);
$doc->appendChild($textareaNode);
//header('Content-Type: text/xml');
echo $doc->save($url);

?>
the above is my getdata.php page

<html>
<body>
<form name="form" action="getdata.php" method="post">
<table>
<tr>
<td>name</td>
<td><input type="text" name="name" /></td>
</tr>
<tr>
<td>age</td>
<td><input type="text" name="age" /></td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" value="Submit" /></td>
</tr>

</table>
</form>
</body>
</html>
the above is my test.php page.

i got the output as

<?xml version="1.0"?>
<order/>
<name>patrick</name>
<age>23</age>

Edited by bavenbabu: n/a

2
Contributors
5
Replies
6
Views
5 Years
Discussion Span
Last Post by bavenbabu
0
<?php
 $url='success.xml';

 $name = 'pritaeas';
 $age = '38';

 $doc = new DOMDocument();
 $root=$doc->createElement("order");
 $doc->appendChild($root);
 
 $textareaNode = $doc->createElement("name");
 $textNode = $doc->createTextNode($name);
 $textareaNode->appendChild($textNode);
 $root->appendChild($textareaNode); // use $root, not $doc
 
 $textareaNode = $doc->createElement("age");
 $textNode = $doc->createTextNode($age);
 $textareaNode->appendChild($textNode);
 $root->appendChild($textareaNode); // use $root, not $doc

 echo $doc->save($url);
?>
0
<?php
 $url='success.xml';

 $name = $_POST['name'];
 $age = $_POST['id'];

 $doc = new DOMDocument();
 $root=$doc->createElement("order");
 $doc->appendChild($root);
 
 $textareaNode = $doc->createElement("name");
 $textNode = $doc->createTextNode($name);
 $textareaNode->appendChild($textNode);
 $root->appendChild($textareaNode); // use $root, not $doc
 
 $textareaNode = $doc->createElement("age");
 $textNode = $doc->createTextNode($age);
 $textareaNode->appendChild($textNode);
 $root->appendChild($textareaNode); // use $root, not $doc

 echo $doc->save($url);
?>

Now i want to insert all names and id in the order list whenever i input values in the respective textboxes.Also i changed
$name = $_POST;
$age = $_POST;;

0

Now i want to insert all names and id in the order list whenever i input values in the respective textboxes.Also i changed
$name = $_POST;
$age = $_POST;;

$doc->getElementsByTagName("order");
//$t = $doc->createElement("title");
$t = $doc->createElement("name");
$t->appendChild($doc->createTextNode($name));

$l = $doc->createElement("age");
$l->appendChild($doc->createTextNode($age));
$title->appendChild($textareaNode);
echo $doc->save("success1.xml");
when i use the above code i am getting the root <order>...</order> repeated.I dont want this root to be shown.

the output is as below:

<?xml version="1.0"?>
<order>
<title>
<name>google</name>
<age>12</age>
</title>
</order>
<order>
<title>
<name>nandhu</name>
<age>23</age>
</title>
</order>

Is there any solution to the above problem

-1

Show your full PHP code and USE CODE TAGS.

this is my test.php page


<html>
<body>
<form name="form" action="getdata1.php" method="post">
<table>
<tr>
<td>name</td>
<td><input type="text" name="name" /></td>
</tr>
<tr>
<td>age</td>
<td><input type="text" name="age" /></td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" value="Submit" /></td>
</tr>

</table>
</form>
</body>
</html>

this is my getdata1.php page
<?php
$name = stripslashes($_POST);
$age = stripslashes($_POST);

$doc= new DOMDocument();
$doc->formatOutput = true;
$doc->load("success1.xml");
//$url='success1.xml';


$root = $doc->createElement("order");
$doc->appendChild($root);
//$doc->appendChild($root);


//$root=$doc->createElement("order");

$title=$doc->createElement("title");
$root->appendChild($title);

$path=$doc->createElement("name");
$text=$doc->createTextNode($name);
$path->appendChild($text);
$text->appendChild($path);
$title->appendChild($path);

$textareaNode = $doc->createElement("age");
$textNode = $doc->createTextNode($age);
$textareaNode->appendChild($textNode);


$title->appendChild($textareaNode);


$doc->getElementsByTagName("order");
$t = $doc->createElement("title");
$t = $doc->createElement("name");
$t->appendChild($doc->createTextNode($name));

$l = $doc->createElement("age");
$l->appendChild($doc->createTextNode($age));
$title->appendChild($textareaNode);
echo $doc->save("success1.xml");

$doc->save("success1.xml");


?>

Votes + Comments
Use code tags !
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.