address.xml
into an XML DOM object.
Then extracts contact
information using JavaScript
<!DOCTYPE html>
<html>
<body>
<h1>TutorialsPoint DOM example </h1>
<div>
<b>Name:</b> <span id = "name"></span><br>
<b>Company:</b> <span id = "company"></span><br>
<b>Phone:</b> <span id = "phone"></span>
</div>
<script>
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
// else
// {// code for IE6, IE5
// xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
// }
xmlhttp.open("GET","address.xml",false);
xmlhttp.send();
xmlDoc = xmlhttp.responseXML;
document.getElementById("name").innerHTML = xmlDoc.getElementsByTagName("name")[0].childNodes[0].nodeValue;
document.getElementById("company").innerHTML = xmlDoc.getElementsByTagName("company")[0].childNodes[0].nodeValue;
document.getElementById("phone").innerHTML = xmlDoc.getElementsByTagName("phone")[0].childNodes[0].nodeValue;
</script>
</body>
</html>
address.xml
file content<?xml version = "1.0"?>
<contact>
<name>Tanmay Patil</name>
<company>TutorialsPoint</company>
<phone>(011) 123-4567</phone>
</contact>
the root
An XML tree
starts at a root element
and branches from the root to child elements
<root>
<child>
<subchild>.....</subchild>
</child>
</root>
The terms parent
, child
, and sibling
are used to describe the relationships between elements.
...
Parents
have children.
Children
have parents.
Siblings
are children on the same level (brothers and sisters).
...
We can use any of the online XML tree viewer tools to see the document's tree structure, like this one:https://www.xmlviewer.org/
Books
XML document to see on tree-viewer<books>
<book category="cooking">
<title lang="en">The goals</title>
<author>Giada De Laurentiis</author>
<year>2007</year>
<price>30.00</price>
</book>
<book category="children">
<title lang="en">12 rules for life</title>
<author>J K. Rowling</author>
<year>2002</year>
<price>29.99</price>
</book>
<book category="web">
<title lang="en">The richest man in Babilon</title>
<author>Erik T. Ray</author>
<year>2001</year>
<price>39.95</price>
</book>
</books>
elements
, attributes
and data types
.
Here is how XML schema looks like<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="company">
<xs:complexType>
<xs:sequence>
<xs:element name="id" type="xs:unsignedInt" />
<xs:element name="name" type="xs:string" />
<xs:element name="phone" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
The XML schema is derived from the following XML:<company>
<id>2859385</id>
<name>Tanmay Patil</name>
<phone>(011) 123-4567</phone>
</company>
The XML schema is generated using the following online tool:https://www.liquid-technologies.com/online-xml-to-xsd-converter
UTF-8
- 8-bits are used to represent a character
2. UTF-16
- 16-bits are used to represent a character
...
UTF
stands for UCS Transformation Format
, and UCS itself means Universal Character Set
...
XML document example that has UTF-8
encoding<?xml version="1.0" encoding="UTF-8"?>
<student>
<first-name>George</first-name>
<phone.mobile>(011) 123-4567</phone.mobile>
</student>
Note: If an XML document has not specified encoding, the default is UTF-8
<?php
$my_xml_data =
'<?xml version="1.0" encoding="UTF-8"?>
<student>
<?sort alpha-ascending?>
<?textinfo whitespace is allowed ?>
<first-name>George</first-name>
<phone.mobile>(011) 123-4567</phone.mobile>
</student>';
$xml = simplexml_load_string($my_xml_data);
$doc = dom_import_simplexml($xml)->ownerDocument;
$xpath = new DOMXPath($doc);
# prints "/* processing instructions */ ", the value of the first PI: textinfo
echo $xpath->evaluate('string(//processing-instruction("textinfo")[1])');
The output of the above code is the following:whitespace is allowed
target
- Identifies the application to which the instruction is directed.
2. instruction
- A character that describes the information for the target
application to process.
...
PI's example in real XML document<?xml version="1.0" encoding="UTF-8"?>
<student>
<?sort alpha-ascending?>
<?textinfo whitespace is allowed ?>
<first-name>George</first-name>
<phone.mobile>(011) 123-4567</phone.mobile>
</student>
In this example we have two PIs
1. sort
with value - alpha-ascending
2. textinfo
with value - whitespace is allowed
...
Note: processing instructions can contain any data except the combination ?>
, which is interpreted as a closing delimiter1. XML Syntactical whitespace - is whitespace required by the XML in order to delimitate XML constructs.
2. Insignificant Whitespace - is whitespace, that is not considered part of an element, if two elements are separated by just whitespace its considered insignificant.
3. Significant Whitespace - is whitespace that XML element attribute contains.
part of the document
, while a comment is not
2. In CDATA we cannot include the string ]]>
, while in a comment --
3. CDATA content is visible on the web if we specify xmlns
attribute as http://www.w3.org/1999/xhtml
, even if the file is saved as .xml
<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>CDATA Example</title>
</head>
<body>
<h2>Using a Comment</h2>
<div id="commentExample">
<!--
You won't see this in the document
and can use reserved characters like
< > & "
-->
</div>
<h2>Using a CDATA Section</h2>
<div id="cdataExample">
<![CDATA[
You will see this in the document
and can use reserved characters like
< > & "
]]>
</div>
</body>
</html>
CDATA
cannot contain the string ]]>
anywhere in the XML document
2. Nesting
is not allowed in CDATA section