IT Community - Software Programming, Web Development and Technical Support

call xml elements in php

This is a discussion on call xml elements in php within the PHP Programming forums, part of the Web Development category; Hi all, How to call the xml elements in php? Regards, vani...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > PHP Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 01-29-2008, 08:52 PM
Vani Sri Vani Sri is offline
D-Web Trainee
 
Join Date: Aug 2007
Posts: 33
Vani Sri is on a distinguished road
Default call xml elements in php

Hi all,
How to call the xml elements in php?
Regards,
vani
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 02-04-2008, 07:15 AM
Kamalakannan Kamalakannan is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 299
Kamalakannan is on a distinguished road
Default Re: call xml elements in php

Hi vani,

Actually what do you want?
__________________
Thanks & Regards,
R.Kamalakannan.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-06-2008, 08:50 PM
Vani Sri Vani Sri is offline
D-Web Trainee
 
Join Date: Aug 2007
Posts: 33
Vani Sri is on a distinguished road
Default Re: call xml elements in php

Hi Kamal,

I want to know how we get the xml elements in php.

<?xml version="1.0" encoding="UTF-8" ?>
<reports>
<project>
Projects - 8
</project>
<task>
Task - 8
</task>
</reports>

from the above example what is the method to get the <task> tag value only.

This is my doubt...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-27-2008, 02:38 AM
Kamalakannan Kamalakannan is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 299
Kamalakannan is on a distinguished road
Default Re: call xml elements in php

Hi Vani,

Using DOM Object we can able to get tag value. Look the below code using this you can get the tag value.

Code:
<?
$oXmlDocument =	new DomDocument;
$oXmlDocument -> preserveWhiteSpace = FALSE;
$oXmlDocument -> loadXML($vXml);
$oTask = $oXmlDocument -> getElementsByTagName('reports');

foreach($oTask as $oTask)
{
	$aTagValues = $oTask->getElementsByTagName('task');
	$vTagValue = $aTagValues->item(0)->nodeValue;
}
echo $vTagValue;
?>
will return as,
Quote:
Out put: Task - 8
__________________
Thanks & Regards,
R.Kamalakannan.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 03-05-2008, 08:19 PM
Vani Sri Vani Sri is offline
D-Web Trainee
 
Join Date: Aug 2007
Posts: 33
Vani Sri is on a distinguished road
Default Re: call xml elements in php

Thanks kamal,
This code works fine in PHP 5
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 03-06-2008, 09:19 PM
senraj senraj is offline
D-Web Master
 
Join Date: Jul 2007
Posts: 418
senraj is on a distinguished road
Post Re: call xml elements in php

Hi,

how to get the xml elements in php.

<?xml version="1.0" encoding="iso-8859-1"?>
<books>
<bookauthor>
<author>Jack Herrington</author>
<author>John Write</author>
</bookauthor>
<booktitle>
<title>PHP Hacks</title>
<title>Podcasting Hacks</title>
</booktitle>
</books>


I need <bookauthor> and <booktitle> tag value only return array.

do you understand my question?

Last edited by senraj : 03-06-2008 at 10:21 PM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 03-07-2008, 08:57 PM
senraj senraj is offline
D-Web Master
 
Join Date: Jul 2007
Posts: 418
senraj is on a distinguished road
Post Re: call xml elements in php

hi,

$doc = new DOMDocument();
$doc->load( 'test.xml' );
$books = $doc->getElementsByTagName( "bookauthor" );
foreach( $books as $book )
{
$authors = $book->getElementsByTagName( "author" );
$aNew = array();
for($i=0;$i<$authors->length;$i++)
{
$author = $authors->item($i)->nodeValue;
$aNew[] = $author;
}
}
print_r( $aNew);
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 03-07-2008, 09:55 PM
senraj senraj is offline
D-Web Master
 
Join Date: Jul 2007
Posts: 418
senraj is on a distinguished road
Post Re: call xml elements in php

hi,

test.xml
--------
<?xml version="1.0" encoding="iso-8859-1"?>
<books>
<bookauthor>
<author>Jack Herrington</author>
<author>John Write</author>
</bookauthor>
<booktitle>
<title>PHP Hacks</title>
<title>Podcasting Hacks</title>
</booktitle>
</books>

i need to added one row <author>Arnold </author> and <title> Javascript </title>.

how to append?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 03-07-2008, 10:48 PM
Vani Sri Vani Sri is offline
D-Web Trainee
 
Join Date: Aug 2007
Posts: 33
Vani Sri is on a distinguished road
Default Re: call xml elements in php

Hi senraj,

Quote:
<?
$doc = new DOMDocument();
$doc->load( 'test.xml' );
$books = $doc->getElementsByTagName( "books" );
foreach( $books as $book )
{
$authors = $book->getElementsByTagName( "author" );
$title = $book->getElementsByTagName( "title" );
$aNew = array();
$i_length=$authors->length;
for($i=0;$i<$i_length;$i++)
{
echo "<br>Author:".$author = $authors->item($i)->nodeValue;
echo "<br>Title:".$titles = $title->item($i)->nodeValue;
}
}
?>
Quote:
Author:Jack Herrington
Title:PHP Hacks
Author:John Write
Title:Podcasting Hacks
Author:Arnold
Title: Javascript
Try this

Last edited by Vani Sri : 03-07-2008 at 10:54 PM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 03-10-2008, 05:30 AM
ursklakshmanan ursklakshmanan is offline
D-Web Trainee
 
Join Date: Aug 2007
Posts: 14
ursklakshmanan is on a distinguished road
Default Re: call xml elements in php

Hi Sen,
Do you want to update the author and title of xml file through php code? if so, let this code may help

$doc = new DOMDocument();
$doc->load( 'test.xml' );
$books = $doc->getElementsByTagName( "books" );


//Checking the existing author
$total=0;
foreach( $books as $book )
{
$authors = $book->getElementsByTagName( "author" );
$i_length=$authors->length;

for($i=0;$i<$i_length;$i++)
{
if($authors->item($i)->nodeValue=="Arnold")
{
$total++;
}

}
}

//Appending the author
if($total==0)
{
$upadate = $doc->getElementsByTagName('bookauthor')->item(0);

$newElement = $doc ->createElement('author');
$txtNode = $doc ->createTextNode ("Arnold");
$newElement -> appendChild($txtNode);
$update -> appendChild($newElement);



//appending the title
$update = $doc->getElementsByTagName('booktitle')->item(0);
$newElement = $doc ->createElement('title');
$txtNode = $doc ->createTextNode ("Javascript");
$newElement -> appendChild($txtNode);
$update -> appendChild($newElement);

$test = $doc->save("test.xml");
}


//To know the result that you updated
foreach( $books as $book )
{

$authors = $book->getElementsByTagName( "author" );
$title = $book->getElementsByTagName( "title" );
$aNew = array();
$i_length=$authors->length;
for($i=0;$i<$i_length;$i++)
{
echo "<br>Author:".$author = $authors->item($i)->nodeValue;
echo "<br>Title:".$titles = $title->item($i)->nodeValue;
}
}
Let me know if an issue regarding in this

எவ்வளவோ பண்ணிடோம் இத பண்ண மாடோமா என்ன!!!
__________________
regards
K.Lakshmanan
[Web developer]
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #11 (permalink)  
Old 03-11-2008, 06:27 AM
senraj senraj is offline
D-Web Master
 
Join Date: Jul 2007
Posts: 418
senraj is on a distinguished road
Post Re: call xml elements in php

hi Lakshmanan,

thank's for your reply.

Last edited by senraj : 03-11-2008 at 06:30 AM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #12 (permalink)  
Old 03-11-2008, 06:29 AM
senraj senraj is offline
D-Web Master
 
Join Date: Jul 2007
Posts: 418
senraj is on a distinguished road
Post Re: call xml elements in php

hi,

<?php

Class books extends domDocument {
function __construct() {
parent::__construct();
}
function addBook($author,$title ) {
$titleElement = $this->createElement("title");
$titleElement->appendChild($this->createTextNode($title));
$authorElement = $this->createElement("author");
$authorElement->appendChild($this->createTextNode($author));
$bookElement = $this->createElement("book");
$bookElement->appendChild($titleElement);
$bookElement->appendChild($authorElement);
$this->documentElement->appendChild($bookElement);
}
}
header("Content-type: text/xml");
$dom = new books();
$dom->load("test.xml");
$dom->addBook("javascript", "arnold");
print $dom->saveXML();

?>
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to Accessing Elements using javascript? itbarota HTML, CSS and Javascript Coding Techniques 1 10-24-2007 08:21 AM
difference between call by value and call by reference amansundar Java Programming 2 09-18-2007 01:34 AM
Function call and System call vigneshgets Operating Systems 1 08-01-2007 06:18 AM
ASP with paging elements kingmaker HTML, CSS and Javascript Coding Techniques 1 07-19-2007 12:32 AM
Form elements tripnautic HTML, CSS and Javascript Coding Techniques 1 03-20-2007 07:40 AM


All times are GMT -7. The time now is 12:29 AM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0