Beruflich Dokumente
Kultur Dokumente
Xml has a root element then it can have child element and attributes together
and child elements can have attribute and child elements.
Consider the below example:
<to>Hume</to>
<from>Havt</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
<to>hooman</to>
<from>Jose</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
This example is wrong because root element (note) has child elements which
are repetatives (from, to, heading, body). To be able to have this
information we should have something like the below xml code:
<email>
<to>Hume</to>
<from>Havt</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</email>
<email>
<to>hooman</to>
<from>Jose</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</email>
</note>
So the root element is note and then the child element is email but we have
three different emails. That is like a one to many relationship in an ER
diagram.
</note>
<email>
<to>Hume</to>
<from>Havt</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</email>
<email>
<to>hooman</to>
<from>Jose</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</email>
</note>
</body>
</html>
The datasrc attribute of the <table> tag binds the HTML table element to the XML
data island. The datasrc attribute refers to the id attribute of the data island.
<td> tags cannot be bound to data, so we are using <span> tags. The <span> tag
allows the datafld attribute to refer to the XML element to be displayed. In this case,
it is datafld="from" for the <from> element and datafld="to" for the <to> element
in the XML file. As the XML is read, additional rows are created for each <emai>
element.
JavaScript:
var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
VBScript:
set xmlDoc=CreateObject("Microsoft.XMLDOM")
Asp:
set xmlDoc=Server.CreateObject("Microsoft.XMLDOM")
The following code fragment loads an existing XML document ("note.xml") into
Microsoft's XML parser:
var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async="false";
xmlDoc.load("note.xml");
The first line of the script above creates an instance of the XML parser. The second
line turns off asynchronized loading, to make sure that the parser will not continue
execution of the script before the document is fully loaded. The third line tells the
parser to load an XML document called "note.xml".
The following example is a cross browser example that loads an existing XML
document ("note.xml") into the XML parser:
<html>
<head>
<script type="text/javascript">
var xmlDoc;
function loadXML()
{
// code for IE
if (window.ActiveXObject)
{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.load("note.xml");
getmessage();
}
// code for Mozilla, Firefox, Opera, etc.
else if (document.implementation &&
document.implementation.createDocument)
{
xmlDoc=document.implementation.createDocument("","",null);
xmlDoc.load("note.xml");
xmlDoc.onload=getmessage;
}
else
{
alert('Your browser cannot handle this script');
}
}
function getmessage()
{
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>
</head>
<body onload="loadXML()">
<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>
</p>
</body>
</html>
To extract the text (Jani) from an XML element like: <from>Jani</from>, the correct
syntax is:
etElementsByTagName("from")[0].childNodes[0].nodeValue
Note: Internet Explorer uses the loadXML() method to parse an XML string, while
Mozilla browsers uses the DOMParser object.
Naming conlicts:
Consider two below xml file:
<table>
<tr>
<td>Apples</td>
<td>Bananas</td>
</tr>
</table>
So what should we do? Clearly we should do something to make them unique. This
can be a way:
<h:table>
<h:tr>
<h:td>Apples</h:td>
<h:td>Bananas</h:td>
</h:tr>
</h:table>
And:
<f:table>
<f:name>African Coffee Table</f:name>
<f:width>80</f:width>
<f:length>120</f:length>
</f:table>
So we would name something called “NAME SPACE” and it means a space in which
the name exists. Now if we have different name spaces then we will have unique
names. Namespace is similar to family name we can have 1000000 john in a city
but belonging to different namespaces or here different family names.
The XML namespace attribute is placed in the start tag of an element and has the
following syntax:
xmlns:namespace-prefix="namespaceURI"
And
<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>
When a namespace is defined in the start tag of an element, all child elements with
the same prefix are associated with the same namespace.
Note that the address used to identify the namespace is not used by the parser to
look up information. The only purpose is to give the namespace a unique name.
However, very often companies use the namespace as a pointer to a real Web page
containing information about the namespace.
If you take a close look at the XSL document below, you will see that most of the
tags are HTML tags. The tags that are not HTML tags have the prefix xsl, identified
by the namespace "http://www.w3.org/1999/XSL/Transform":
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr>
<th align="left">Title</th>
<th align="left">Artist</th>
</tr>
<xsl:for-each select="catalog/cd">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Only the characters "<" and "&" are strictly illegal in XML. Apostrophes, quotation
marks and greater than signs are legal, but it is a good habit to replace them.
In the example above, everything inside the CDATA section is ignored by the parser.
A CDATA section cannot contain the string "]]>", therefore, nested CDATA sections
are not allowed.
Also make sure there are no spaces or line breaks inside the "]]>" string.
Encoding attribute:
All you have to do is to add some <span> or <div> elements to your page. Use the
datasrc attribute to bind the elements to the Data Island, and the datafld attribute
to bind each element to an XML element, like this:
<br />Title:
<span datasrc="#xmldso" datafld="TITLE"></span>
<br />Artist:
<span datasrc="#xmldso" datafld="ARTIST"></span>
<br />Year:
<span datasrc="#xmldso" datafld="YEAR"></span>
Or like this:
<br />Title:
<div datasrc="#xmldso" datafld="TITLE"></div>
<br />Artist:
<div datasrc="#xmldso" datafld="ARTIST"></div>
<br />Year:
<div datasrc="#xmldso" datafld="YEAR"></div>
<head>
<script type="text/javascript">
function testclick(field)
var row=field.rowIndex
xmldso_list.recordset.absoluteposition=row
td_title.innerHTML=xmldso_list.recordset("TITLE")
td_artist.innerHTML=xmldso_list.recordset("ARTIST")
td_year.innerHTML=xmldso_list.recordset("YEAR")
td_country.innerHTML=xmldso_list.recordset("COUNTRY")
td_company.innerHTML=xmldso_list.recordset("COMPANY")
td_price.innerHTML=xmldso_list.recordset("PRICE")
</script>
</head>
<body>
</table>
<thead>
<tr align="left">
<th>Title</th>
<th>Artist</th>
<th>Country</th>
<th>Company</th>
<th>Price</th>
<th>Year</th>
</tr>
</thead>
<tr align="left" onclick="testclick(this)">
</tr>
</table>
</body>
</html>
<head>
<script type="text/javascript">
var xmlhttp
function loadXMLDoc(url)
xmlhttp=null
if (window.XMLHttpRequest)
xmlhttp=new XMLHttpRequest()
// code for IE
else if (window.ActiveXObject)
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
}
if (xmlhttp!=null)
xmlhttp.onreadystatechange=state_Change
xmlhttp.open("GET",url,true)
xmlhttp.send(null)
else
function state_Change()
if (xmlhttp.readyState==4)
// if "OK"
if (xmlhttp.status==200)
document.getElementById('T1').innerHTML=xmlhttp.responseText
else
</script>
</head>
<body onload="loadXMLDoc('test_xmlhttp.txt')">
<button onclick="loadXMLDoc('test_xmlhttp2.txt')">Click</button>
</body>
</html>
Methods
Method Description
Properties
Property Description
0 = uninitialized
1 = loading
2 = loaded
3 = interactive
4 = complete
status Returns the status as a number (e.g. 404 for "Not Found" or
200 for "OK")