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...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| 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... |
| |||
| 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;
?> Quote:
__________________ Thanks & Regards, R.Kamalakannan. |
| |||
| 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. |
| |||
| 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); |
| |||
| 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? |
| |||
| Hi senraj, Quote:
Quote:
![]() Last edited by Vani Sri : 03-07-2008 at 10:54 PM. |
| |||
| 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] |
| |||
| 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(); ?> |
![]() |
| Thread Tools | |
| Display Modes | |
| |
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 |