PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Hi,
I scanned the google results for XML DOM, and found a couple of tutorials. One on [url]http://www.ibm.com/developerworks/library//x-xmlphp1.html[/url], and another from [url]http://devzone.zend.com/article/2387-XML-and-PHP-5[/url]. Using a little bit of both, I pieced together what I think should work to create a new XML file:
<?php
//New XML
$xml = new DOMDocument('1.0');
//specify the root element
$root = $xml->append_child($xml->create_element('users'));
//Add new user
$user = $root->append_child($xml->create_element('user'));
//Add the email
$email = $user->append_child($xml->create_element('email'));
//Add the contents of $email
$email->append_child($xml->create_text_node('jonah@nucleussystems.com'));
//format xml
$root->format_output = true;
//Save xml
$root->save('file.xml');
?>
And this is the error I get when it is uploaded and tested:
Warning: domdocument(): Start tag expected, '<' not found in /home/content/g/e/i/geinternet/html/nucleussystems/XML/index.php on line 3
Warning: create_element(): Underlying object missing in /home/content/g/e/i/geinternet/html/nucleussystems/XML/index.php on line 6
Warning: create_element(): Cannot fetch DOM object in /home/content/g/e/i/geinternet/html/nucleussystems/XML/index.php on line 6
Fatal error: Call to a member function on a non-object in /home/content/g/e/i/geinternet/html/nucleussystems/XML/index.php on line 9
Why does this error occur?
Any help much appreciated
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
[quote="[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1"][b]1.[/b] Select the correct board for your query. Take some time to read the guidelines in the sticky topic.[/quote]
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Another question:
I tried, but couldn't figure out how to access the already-existing root, and an already existing child? (and it's content, and attributes)
One more question: (and hes, this is the last question)
I'm trying to use PHP to read a manually made index for my site, and gives an output if the description, title, or keywords include the searched string.
Warning: domdocument(): Start tag expected, '<' not found in /home/content/g/e/i/geinternet/html/nucleussystems/search/index.php on line 2
Fatal error: Call to undefined function: load() in /home/content/g/e/i/geinternet/html/nucleussystems/search/index.php on line 3
<?php
$xml = new DOMDocument('1.0');//New Document
$xml->load('search.xml');//Load "search.xml"
$xml->formatOutput = true;//format output
//Set tree
$root = $xml->documentElement;//root
$page = $root->firstChild;//each page
$doc = $page->firstChild;//each property of each page
while ($page){//while there is a node to examin
if ($page->nodeType==XML_ELEMENT_NODE && $page->nodeName=='page'){//if nodetype is correct, and nodename is "page"
//if any of the properties include the search string,
if (($page->firstChild->nodeValue.eregi($search)) || $page->firstChild->nextSibling->nodeValue.eregi($search)) || ($page->firstChild->nextSibling->nextSibling->nodeValue.eregi($search))){
while ($doc){//loop through properties
if ($doc->nodeType==XML_ELEMENT_NODE){//if nodetype is correct
if ($doc->nodeName=='name'){//if name is "name"
//output title, as a link to the "address" property
echo '<h3><a href="'.$doc->nextSibling->nextSibling->nextSibling->firstChild->nodeValue.'"">'.$doc->firstChild->nodeValue.'</a></h3>';
}elseif ($doc->nodeName=='desc'){//if name is "desc"
//output description
echo '<blockquote>'.$doc->firstChild->nodeValue.'</blockquote><br />';
}elseif ($page->nodeName=='address'){//if name is "address"
//output address, as a link
echo '<a href="'.$doc->firstChild->nodeValue.'">'.$doc->firstChild->nodeValue.'</a>';
}
}
//goto next sibling
$doc = $doc->nextSibling;
}
}
}
//goto next sibling
$page = $page->nextSibling;
}
//close xml doc
$xml->saveXML();
?>
<?xml version="1.0" ?>
<index>
<page>
<name>jBlog home page</name>
<desc>Learn XML HTML PHP</desc>
<keys>Learn XML PHP HTML CSS JS web design</keys>
<address>http://nucleussystems.com/blog</addresss>
</page>
</index>