Introduction - To - XML Sem6 Bca
Introduction - To - XML Sem6 Bca
print $xmlDoc->saveXML();
?>
• The output of the code above will be:
• Tove Jani Reminder Don't forget me this weekend
• If you select "View source" in the browser window, you will
see the following HTML:
• <?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
• The example above creates a DOMDocument-Object and
loads the XML from "note.xml" into it.
• The XML DOM(XML Document Object Model) :
• The XML DOM defines a standard way for accessing and
manipulating XML documents.
• PHP DOMs functions
• i.domxml_new_doc(): to create new XML document.
•
• ii.domxml_open_file(): to open an XML document file as a DOM
object.
• iv. create_child(): creates new element.
•
• v. append_child(): Which appends an element as a child of an
existing element.
•
• Vi DomDocument->get_elements_by_tagname: Returns array with
nodes with given tagname in document or empty array,if not found.
• <book>
• <title>
• <year>
• </year>
• </title>
• </book>
• Calling:
• $domobj->get_elements_by_tagname(“title”);
• The Node Object
• The Node object represents a single node in the document
tree.
• A node can be an element node, an attribute node, a text
node, or any other of the node
• Get an Element Value
• The getElementsByTagName() method returns a node list
containing all elements with the specified tag name in the
same order as they appear in the source document.
• xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title")[0];
• Get the Value of an Attribute
• In the DOM, attributes are nodes. Unlike element nodes, attribute
nodes have text values.
• The way to get the value of an attribute, is to get its text value.
• This can be done using the getAttribute() method or using the
nodeValue property of the attribute node.
• Get an Attribute Value - getAttribute()
• The getAttribute() method returns an attribute value.
• The following code retrieves the text value of the "lang" attribute of
the first <title> element:
• Example
• xmlDoc=loadXMLDoc("books.xml");
txt=xmlDoc.getElementsByTagName("title")[0].getAttribute("lang");
• Result: txt = "en"
• Get an Attribute Value - getAttributeNode()
• The getAttributeNode() method returns an attribute node.
• The following code retrieves the text value of the "lang"
attribute of the first <title> element:
• Example
• xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title")
[0].getAttributeNode("lang");
txt=x.nodeValue;
• Result: txt = "en"