<!-- /* Font Definitions */ @font-face {font-family:Wingdings; panose-1:5 0 0 0 0 0 0 0 0 0; mso-font-charset:2; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:0 268435456 0 0 -2147483648 0;} @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"/@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0in; margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋体;} h2 {mso-margin-top-alt:auto; margin-right:0in; mso-margin-bottom-alt:auto; margin-left:0in; mso-pagination:widow-orphan; mso-outline-level:2; font-size:18.0pt; font-family:"Times New Roman"; font-weight:bold;} a:link, span.MsoHyperlink {color:blue; text-decoration:underline; text-underline:single;} a:visited, span.MsoHyperlinkFollowed {color:purple; text-decoration:underline; text-underline:single;} p {mso-margin-top-alt:auto; margin-right:0in; mso-margin-bottom-alt:auto; margin-left:0in; mso-pagination:widow-orphan; font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋体;} @page Section1 {size:595.3pt 841.9pt; margin:1.0in 1.25in 1.0in 1.25in; mso-header-margin:.5in; mso-footer-margin:.5in; mso-paper-source:0;} div.Section1 {page:Section1;} /* List Definitions */ @list l0 {mso-list-id:328367209; mso-list-template-ids:353637624;} @list l0:level1 {mso-level-number-format:bullet; mso-level-text:; mso-level-tab-stop:.5in; mso-level-number-position:left; text-indent:-.25in; mso-ansi-font-size:10.0pt; font-family:Symbol;} @list l1 {mso-list-id:396826914; mso-list-template-ids:-1310454738;} @list l1:level1 {mso-level-number-format:bullet; mso-level-text:; mso-level-tab-stop:.5in; mso-level-number-position:left; text-indent:-.25in; mso-ansi-font-size:10.0pt; font-family:Symbol;} @list l2 {mso-list-id:1594244381; mso-list-template-ids:-1764979160;} @list l2:level1 {mso-level-number-format:bullet; mso-level-text:; mso-level-tab-stop:.5in; mso-level-number-position:left; text-indent:-.25in; mso-ansi-font-size:10.0pt; font-family:Symbol;} ol {margin-bottom:0in;} ul {margin-bottom:0in;} -->
Entity References
If you place a character like “<” inside an XML element, it will generate an error because the parser interprets it as the start of a new element.
This will generate an XML error:
< message > if salary < 1000 then </ message >
To avoid this error, replace the “<” character with an entity reference :
< message > if salary < 1000 then </ message >
There are 5 predefined entity reference in XML:
< | < | Less than |
> | > | Greater than |
& | & | ampersand |
' | ‘ | apostrophe |
" | “ | quotation mark |
Best Naming Practices
Make names descriptive. Names with an underscore separator are nice: <first_name>, <last_name>.
Names should be short and simple, like this: <book_title> not like this: <the_title_of_the_book>.
Avoid "-" characters. If you name something "first-name," some software may think you want to subtract name from first.
Avoid "." characters. If you name something "first.name," some software may think that "name" is a property of the object "first."
Avoid ":" characters. Colons are reserved to be used for something called namespaces (more later).
XML documents often have a corresponding database. A good practice is to use the naming rules of your database for the elements in the XML documents.
Non-English letters like éòá are perfectly legal in XML, but watch out for problems if your software vendor doesn't support them.
XML Attributes Must be Quoted
Attribute value should have single or double quotes. Either is ok.
< person sex =" female ">
or
< person sex =' female '>
If the attribute value itself contains double quotes you can use single quotes, like this:
< gangster name =' George "Shotgun" Ziegler '>
or
< gangster name =" George "Shotgun" Ziegler ">
XML element vs attribute
Use element or attribute in xml is fine based on syntax. So you can use:
< note date =" 10/01/2008 ">
< to > Tove </ to >
< from > Jani </ from >
< heading > Reminder </ heading >
< body > Don't forget me this weekend! </ body >
</ note >
or
< note >
< date > 10/01/2008 </ date >
< to > Tove </ to >
< from > Jani </ from >
< heading > Reminder </ heading >
< body > Don't forget me this weekend! </ body >
</ note >
But my favorite way is :
< note >
< date >
< day > 10 </ day >
< month > 01 </ month >
< year > 2008 </ year >
</ date >
< to > Tove </ to >
< from > Jani </ from >
< heading > Reminder </ heading >
< body > Don't forget me this weekend! </ body >
</ note >
well-formed xml means roles of xml parser. Such as: xml document must have a root element, closing tag, case sensitive, attribute should be quoted, etc.
valid xml means it is well-formed xml and follow its DTD validation.
Parsing XML
The XML DOM(Document Object Model) contains methods (functions) to traverse XML trees, access, insert, and delete nodes.
However, before an XML document can be accessed and manipulated, it must be loaded into an XML DOM object.
The parser reads XML into memory and converts it into an XML DOM object that can be accessed with JavaScript.
Books.XML
< bookstore >
−
< book category =" COOKING ">
< title lang =" en "> Everyday Italian </ title >
< author > Giada De Laurentiis </ author >
< year > 2005 </ year >
< price > 30.00 </ price >
</ book >
−
< book category =" CHILDREN ">
< title lang =" en "> Harry Potter </ title >
< author > J K. Rowling </ author >
< year > 2005 </ year >
< price > 29.99 </ price >
</ book >
−
< book category =" WEB ">
< title lang =" en "> XQuery Kick Start </ title >
< author > James McGovern </ author >
< author > Per Bothner </ author >
< author > Kurt Cagle </ author >
< author > James Linn </ author >
< author > Vaidyanathan Nagarajan </ author >
< year > 2003 </ year >
< price > 49.99 </ price >
</ book >
−
< book category =" WEB ">
< title lang =" en "> Learning XML </ title >
< author > Erik T. Ray </ author >
< year > 2003 </ year >
< price > 39.95 </ price >
</ book >
</ bookstore >
Load an XML Document
The following JavaScript fragment loads an XML document ("books.xml "):
var xmlDoc;
xmlDoc=new window.XMLHttpRequest();
xmlDoc.open("GET","books.xml",false);
xmlDoc.send("");
Code explained:
- Create an XMLHTTP object
- Open the XMLHTTP object
- Send an XML HTTP request to the server
Load an XML String
The following code loads and parses an XML string:
try //Internet Explorer
{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async="false";
xmlDoc.loadXML(txt);
return xmlDoc;
}
catch(e)
{
parser=new DOMParser();
xmlDoc=parser.parseFromString(txt,"text/xml");
return xmlDoc;
}
The XML DOM
In the examples below we use the following DOM reference to get the text from the <to> element:
xmlDoc.getElementsByTagName("to")[0].childNodes[0].nodeValue
- xmlDoc - the XML document created by the parser.
- getElementsByTagName("to")[0] - the first <to> element
- childNodes[0] - the first child of the <to> element (the text node)
- nodeValue - the value of the node (the text itself)
The HTML DOM
In the examples below we use the following DOM reference to change the text of the HTML element where id="to":
document.getElementById("to").innerHTML=
- document - the HTML document
- getElementById("to") - the HTML element where id="to"
- innerHTML - the inner text of the HTML element
Load an XML File - A Cross browser Example
The following code loads an XML document ("note.xml") into the XML parser:
<html>
<head>
<script type="text/javascript">
function loadXMLDoc(dname)
{
var xmlDoc;
if (window.XMLHttpRequest)
{
xmlDoc=new window.XMLHttpRequest();
xmlDoc.open("GET",dname,false);
xmlDoc.send("");
return xmlDoc.responseXML;
}
// IE 5 and IE 6
else if (ActiveXObject("Microsoft.XMLDOM"))
{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.load(dname);
return xmlDoc;
}
alert("Error loading document");
return null;
}
</script>
</head>
<body>
<h1>W3Schools Internal Note</h1>
<p><b>To:</b> <span id="to"></span><br />
<b>From:</b> <span id="from"></span><br />
<b>Message:</b> <span id="message"></span>
<script type="text/javascript">
xmlDoc=loadXMLDoc("note.xml");
document.getElementById("to").innerHTML= xmlDoc.getElementsByTagName("to")[0].childNodes[0].nodeValue;
document.getElementById("from").innerHTML= xmlDoc.getElementsByTagName("from")[0].childNodes[0].nodeValue;
document.getElementById("message").innerHTML= xmlDoc.getElementsByTagName("body")[0].childNodes[0].nodeValue;
</script>
</body>
</html>
XML Namespaces - The xmlns Attribute
The namespace is defined by the xmlns attribute in the start tag of an element.
The namespace declaration has the following syntax. xmlns:prefix ="URI ".
< root >
< h:table xmlns:h =" http://www.w3.org/TR/html4/ ">
< h:tr >
< h:td > Apples </ h:td >
< h:td > Bananas </ h:td >
</ h:tr >
</ h:table >
< f:table xmlns:f =" http://www.w3schools.com/furniture ">
< f:name > African Coffee Table </ f:name >
< f:width > 80 </ f:width >
< f:length > 120 </ f:length >
</ f:table >
</ root >
The xmlns attribute in the <table> tag give the h: and f: prefixes a qualified namespace.
The purpose is to give the namespace a unique name. However, often companies use the namespace as a pointer to a web page containing namespace information.
Uniform Resource Identifier (URI)
A Uniform Resource Identifier (URI) is a string of characters which identifies an Internet Resource.
The most common URI is the Uniform Resource Locator (URL) which identifies an Internet domain address. Another, not so common type of URI is the Universal Resource Name (URN).
CDATA - (Unparsed) Character Data
Like javascript, has a lot of “<” and “&”. And these characters are not legal in XML. To avoid errors script code can be defined as CDATA.
A CDATA section starts with "<![CDATA[ " and ends with "]]> ":
< script >
<![CDATA[
function matchwo(a,b)
{
if (a < b && a < 0) then
{
return 1;
}
else
{
return 0;
}
}
]]>
</ script >