Jump to content

Simple PHP Call


Kovo

Recommended Posts

What would a PHP script look for displaying all <SITE> within the <INFORMATION> category?<?xml version="1.0" encoding="ISO-8859-1"?><?xml-stylesheet type="text/css" href="css2.css"?><SITES><INFOBLOG><SITE><NAME>Kovo's Webspace</NAME><LINK url="http://www.kovo.ca">http://www.kovo.ca</LINK><DESC>An online blog.</DESC></SITE></INFOBLOG><WEBDESIGN><SITE><NAME>Kevmedia</NAME><LINK url="http://www.kevmedia.ca">http://www.kovo.ca</LINK><DESC>A Montreal based, Web Design company.</DESC></SITE></WEBDESIGN></SITES>Im a complete noob to this, and was just wondering if it was simple. Thanks

Link to comment
Share on other sites

What version of PHP are you using?Also, I do not see a <INFORMATION> node in your xml, so I am not sure which nodes you want to pull out. Did you mean all <SITE> nodes in the <SITES> node?

Link to comment
Share on other sites

What version of PHP are you using?Also, I do not see a <INFORMATION> node in your xml, so I am not sure which nodes you want to pull out. Did you mean all <SITE> nodes in the <SITES> node?
Sorry, I meant <INFOBLOG>Using PHP4 or 5 dsnt matter
Link to comment
Share on other sites

If you are using PHP 5, you could do something like:

$xml = new DomDocument;$xml->load($xml_file);$xml_obj = simplexml_import_dom($xml);print($xml_obj->INFOBLOG->SITE->NAME);unset($xml, $xml_obj);

This will print "Kovo's Webspace" to the page. Using the SimpleXML object it makes it easy to traverse the nodes of the xml doc.If you want to loop through the <SITE> nodes, try something like:

foreach ($xml_obj->INFOBLOG->children() as $child){	if($child->getName() == "SITE")		print($child->NAME);}

Link to comment
Share on other sites

If you are using PHP 5, you could do something like:
$xml = new DomDocument;$xml->load($xml_file);$xml_obj = simplexml_import_dom($xml);print($xml_obj->INFOBLOG->SITE->NAME);unset($xml, $xml_obj);

This will print "Kovo's Webspace" to the page. Using the SimpleXML object it makes it easy to traverse the nodes of the xml doc.If you want to loop through the <SITE> nodes, try something like:

foreach ($xml_obj->INFOBLOG->children() as $child){	if($child->getName() == "SITE")		print($child->NAME);}

Warning: DOMDocument::load() [function.DOMDocument-load]: I/O warning : failed to load external entity "/mnt/w0108/d41/s44/b02877b8/www/thekovonetwork.net/xml" in /mnt/w0108/d41/s44/b02877b8/www/thekovonetwork.net/infoblog.php on line 4Warning: simplexml_import_dom() [function.simplexml-import-dom]: Invalid Nodetype to import in /mnt/w0108/d41/s44/b02877b8/www/thekovonetwork.net/infoblog.php on line 5
<?php$xml = new DomDocument;$xml->load($sites.xml);$xml_obj = simplexml_import_dom($xml);print($xml_obj->INFOBLOG->SITE->NAME);unset($xml, $xml_obj);?>

Link to comment
Share on other sites

Make sure your xml is well-formed. You can do this by opening the file in IE. It should give you an error message if the xml is not well-formed.Here is some xml I have used with SimpleXML before:

<?xml version="1.0" encoding="utf-8"?><Screen>	<UserName />	<BillID />	<Comment />	<Errors /></Screen>

Link to comment
Share on other sites

There would also be a problem with this:

$xml->load($sites.xml);

I don't know what's in $sites, but I guess it's the beginning of the filename, change it to one of the below:

$xml->load($sites . '.xml');or$xml->load('sites.xml');

(The value of the var. $sites with the .xml extension added, or a file called sites.xml)But you don't need DOM to open a file to then use with SimpleXML (Constructor info), just open it with SimpleXML from the beginning:

<?php$xml = new SimpleXMLElement('sites.xml', NULL, true);print($xml->INFOBLOG->SITE->NAME);unset( $xml );?>

Good Luck and Don't Panic!

Link to comment
Share on other sites

There would also be a problem with this:
$xml->load($sites.xml);

I don't know what's in $sites, but I guess it's the beginning of the filename, change it to one of the below:

$xml->load($sites . '.xml');or$xml->load('sites.xml');

(The value of the var. $sites with the .xml extension added, or a file called sites.xml)But you don't need DOM to open a file to then use with SimpleXML (Constructor info), just open it with SimpleXML from the beginning:

<?php$xml = new SimpleXMLElement('sites.xml', NULL, true);print($xml->INFOBLOG->SITE->NAME);unset( $xml );?>

Good Luck and Don't Panic!

Thanks that lst code worked, but now, how do i loop through all the site child nodes in INFOBLOG only and not the restFor example, I want all SITE in INFOBLOG to display but not SITE in <WEBD><?xml version="1.0" standalone="yes"?><SITES> <INFOBLOG> <SITE> <NAME>Kovo's Webspace</NAME> <LINK url="http://www.kovo.ca">http://www.kovo.ca</LINK>'>http://www.kovo.ca">http://www.kovo.ca</LINK> <DESC>An online blog.</DESC> </SITE> <SITE> <NAME>Kovo's Webspace 2</NAME> <LINK url="http://www.kovo.ca 2">http://www.kovo.ca2</LINK> <DESC>An online blog.2</DESC> </SITE> </INFOBLOG> <WEBD> <SITE> <NAME>Kovo's Webspace</NAME> <LINK url="http://www.kovo.ca">http://www.kovo.ca</LINK>'>http://www.kovo.ca">http://www.kovo.ca</LINK> <DESC>An online blog.</DESC> </SITE> <SITE> <NAME>Kovo's Webspace 2</NAME> <LINK url="http://www.kovo.ca 2">http://www.kovo.ca2</LINK> <DESC>An online blog.2</DESC> </SITE> </WEBD> </SITES>
Link to comment
Share on other sites

You can go thru them like this:

...echo "<ul>\n";foreach ($xml->INFOBLOG->SITE as $site) {	 // Work with the SITE in $site:	 echo '<li><a href="'.$site->LINK.'" title="'.$site->DESC.'">'.$site->NAME.' - '.$site->DESC."</a></li>\n";}echo "</ul>\n";

Note: I ignored the attribute url to LINK (I used the content of LINK as the url), as I couldn't figure out the reason for the url-attribute...To get the attribute use the following:

$site->LINK['url']

Link to comment
Share on other sites

You can go thru them like this:
...echo "<ul>\n";foreach ($xml->INFOBLOG->SITE as $site) {	 // Work with the SITE in $site:	 echo '<li><a href="'.$site->LINK.'" title="'.$site->DESC.'">'.$site->NAME.' - '.$site->DESC."</a></li>\n";}echo "</ul>\n";

Note: I ignored the attribute url to LINK (I used the content of LINK as the url), as I couldn't figure out the reason for the url-attribute...To get the attribute use the following:

$site->LINK['url']

cool thanks man
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...