How to : Create Element & Get Elements from XML File using
PHPHandling
XML files using PHP is very simple and there are functions available to do these
tasks. You can create elements and then get the elements using the functions in
PHP. We will see an example on how to create element and then get that element
and display it in the browser.
_______________________________________________
_______________________________________________
Creating
an element To
create a new element in an XML file you have to use the createElement function.
The name of the element to be created is passed as a parameter to this function.
You can also pass the value of that element as a parameter. Although you can add
the new element using the createElement function, you cannot see it in the XML
file unless you use the appendChild() function to insert it in the XML document.
If there is any error in creating the element the function would return FALSE. Consider
the XML file candidate.xml that is given below: <?xml
version="1.0" encoding="UTF-8"?> <candidate>
<candidatename>Robert</candidatename> <candidatecity>New
York</candidatecity> <SSN>20072007</SSN> <organization>XYZ
Corp</organization> </candidate> In
the above file we will add a new element called candidatedesignation. Consider
the php code given below: <?php $xdoc
= new DomDocument; $xdoc->Load('C:/php/xml_files/candidate.xml'); $candidate
= $xdoc->getElementsByTagName('candidate')->item(0); $newElement = $xdoc
->createElement('candidatedesignation'); $candidate -> appendChild($newElement); $test
= $xdoc->save("C:/php/xml_files/candidate2.xml"); echo "<B>New
Element Created<B>" ?> After
running the above code you will find that a new element has been added to the
XML file as given below: <?xml
version="1.0" encoding="UTF-8"?> <candidate>
<candidatename>Robert</candidatename> <candidatecity>New
York</candidatecity> <SSN>20072007</SSN> <organization>XYZ
Corp</organization> <candidatedesignation/> </candidate> If
you want to add text to that element you might use the createTextNode function.
Get
Elements from an XML file To
get the elements from an XML file you can use the getElementsByTagName function.
This function takes the name of the element that is to be searched for in the
XML file. The parameter passed is a string. This function returns all the elements
with the name passed as parameter in the form of a DOMNodeList object. To return
all the elements in the XML document you can use the "*" instead of
a name as a parameter to the function. Consider
an XML file candidate.xml that is given below: <?xml
version="1.0" encoding="UTF-8"?> <candidate>
<candidatename id='candid'>Robert</candidatename> <candidatecity>New
York</candidatecity> <SSN>20072007</SSN> <organization>XYZ
Corp</organization> </candidate> From
the above XML file you can get the element candidatename and then gets its attribute.
This is given in the sample code below: <?php $xdoc
= new DomDocument; $xdoc->Load('C:/php/xml_files/candidate.xml'); $candidatename
= $xdoc->getElementsByTagName('candidatename')->item(0); echo "<HTML><Head>"; echo
"<title> Getting Elements By Tag Name</title>"; echo
"</Head><body><B>"; echo "Attribute Name is
:".$candidatename->getAttribute('id'); echo "</B></body></HTML>"; ?> Thus
you can use the functions available in PHP to create elements and then to get
the elements from an XML file. These codes are very simple and you can try these
on your own to get a feel on how easy it is to work with the XML files using PHP.
_______________________________________________
_______________________________________________
FREE
Subscription
Subscribe to our mailing list and receive new articles
through email. Keep yourself updated with latest
developments in the industry.
Note
: We never rent, trade, or sell my email lists to
anyone.
We assure that your privacy is respected
and protected.
_______________________________________
Recommended
XML Books
| |