Jump to content

php create XML


optikalefx

Recommended Posts

ive been bangin my head against a wall on this one

 

 

i have an xml document.

I want to use php to add a node to that document.

 

here is links.xml

<pages>
<link>
<title>words</title>
<url>words</title>
</link>
<link>
<title>words</title>
<url>words</title>
</link>
</pages>

 

I want to add another

<link>
<title>words</title>
<url>words</title>
</link>

 

so far in php i get these variables from a form

$fname = $_POST["fname"];

$lname = $_POST["lname"];

$email = $_POST["email"];

$address = $_POST["address"];

$pnum = $_POST["pnum"];

 

the format im aiming for is in the xml document each variable (except email) is going to be in <title> paired with the same $email.

for example

 

<title>$address</title>
<url>$email</title>
</link>
<link>
<title>$pnum</title>
<url>$email</title>
</link>

 

I tried going thruoug the manual over and over again and i cant find out how to append the xml document.

i found these, but dont work

$xmlDoc = new DOMDocument();
$xmlDoc->loadXML("links.xml");

$node1 = $xmlDoc->create_element("link");
$newnode1 = $xmlDoc->append_child($node1);

$node2 = $newnode1->create_element("title");
$newnode2 = $newnode1->append_child($node2);
$node2->set_attribute("align", "left");

$node3 = $newnode1->create_element("url");
$newnode3 = $newnode1->append_child($node3);
$node3->set_attribute("align", "left");

 

 

any ideas?

Link to comment
https://forums.phpfreaks.com/topic/72063-php-create-xml/
Share on other sites

since no one wanted to respond, i figured out half way.

<?php

$doc = new DOMDocument();
$doc->preserveWhitespace = false;
$doc->load('xmltest.xml');
// we want a nice output
$xpath = new DOMXPath($doc);
$doc->formatOutput = true;


/* THIS NEEDS TO GET TAG <PAGES> AND STORE IT TO $ROOT */
$root = $doc->createElement('pages');
/* THIS NEEDS TO GET TAG <PAGES> AND STORE IT TO $ROOT */



$root = $doc->appendChild($root);

$inner = $doc->createElement('book');
$inner = $root->appendChild($inner);

$title = $doc->createElement('title');
$title = $inner->appendChild($title);

$text = $doc->createTextNode('This is the title');
$text = $title->appendChild($text);


echo 'Wrote: ' . $doc->save("xmltest.xml") . ' bytes'; // Wrote: 72 bytes


?>

 

 

that one line i commented around.

 

I need to some how getElement pages.

 

This whole thing works without that line, except it just creates new tags

I need to create those tags inside of <pages>

 

Link to comment
https://forums.phpfreaks.com/topic/72063-php-create-xml/#findComment-363168
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.