| Author: UltimateRengan 17 Jul 2008 | Member Level: Diamond | Rating: Points: 1 |
<html> <body>
<script type="text/javascript"> var xmlDoc=null; if (window.ActiveXObject) {// code for IE xmlDoc=new ActiveXObject("Microsoft.XMLDOM"); } else if (document.implementation.createDocument) {// code for Mozilla, Firefox, Opera, etc. xmlDoc=document.implementation.createDocument("","",null); } else { alert('Your browser cannot handle this script'); } if (xmlDoc!=null) { xmlDoc.async=false; xmlDoc.load("cd_catalog.xml");
document.write("<table border='1'>");
var x=xmlDoc.getElementsByTagName("CD"); for (i=0;i<x.length;i++) { document.write("<tr>"); document.write("<td>"); document.write( x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue); document.write("</td>");
document.write("<td>"); document.write( x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue); document.write("</td>"); document.write("</tr>"); } document.write("</table>"); } </script>
</body> </html>
more reference login to:- http://www.w3schools.com/xml/xml_to_html.asp http://www.expertrating.com/courseware/XMLCourse/XML-Write-Xml-Document-3.asp http://www.developerfusion.co.uk/show/3944/
UltimateRengan nathan.rengan@gmail.com Trichy-Rider Group
|
| Author: Shivshanker Cheral 17 Jul 2008 | Member Level: Diamond | Rating: Points: 0 |
XML File write: element, comments
<%@ Page Language="C#" %> <%@ Import Namespace="System.Xml" %>
<script runat="server"> void Page_Load(object sender, EventArgs e) { //string xmlFilePath = @"C:\Data\Employees.xml"; string xmlFilePath = MapPath("EmployeesNew.xml"); try { using (XmlWriter writer = XmlWriter.Create(xmlFilePath)) { writer.WriteStartDocument(false); writer.WriteComment("This XML file represents the details of an employee"); //Start with the root element writer.WriteStartElement("employees"); writer.WriteStartElement("employee"); writer.WriteAttributeString("id", "1"); writer.WriteStartElement("name"); writer.WriteElementString("firstName", "Nancy"); writer.WriteElementString("lastName", "Lee"); writer.WriteEndElement(); writer.WriteElementString("city", "Seattle"); writer.WriteElementString("state", "WA"); writer.WriteElementString("zipCode", "98122"); writer.WriteEndElement(); writer.WriteEndElement(); writer.WriteEndDocument(); //flush the object and write the XML data to the file writer.Flush(); lblResult.Text = "File is written successfully"; } } catch (Exception ex) { lblResult.Text = "An Exception occurred: " + ex.Message; } }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Writing XML File</title> </head> <body> <form id="form1" runat="server"> <div> <asp:label id="lblResult" runat="server" /> </div> </form> </body> </html>
|
| Author: Sabu C Alex 18 Jul 2008 | Member Level: Gold | Rating: Points: 6 |
void CreateXmlFile(String xmlFilePath) { XmlTextWriter xmlWriter = new XmlTextWriter(xmlFilePath, Encoding.UTF8); xmlWriter.WriteStartDocument(true); xmlWriter.WriteStartElement("Departments"); //Root Element xmlWriter.WriteStartElement("Department"); //Department Element xmlWriter.WriteStartAttribute("Name"); //Attribute "Name" xmlWriter.WriteString("Development"); //Attribute Value xmlWriter.WriteEndAttribute(); xmlWriter.WriteStartElement("Employees"); //Started Employees Element xmlWriter.WriteStartElement("Employee"); //Started Employee Element xmlWriter.WriteStartAttribute("Name"); //Attribute "Name" xmlWriter.WriteString("Sabu C.Alex"); //Attribute Value xmlWriter.WriteEndAttribute(); xmlWriter.WriteStartAttribute("Age");//Attribute "Age" xmlWriter.WriteString("28");//Attribute Value xmlWriter.WriteEndAttribute(); xmlWriter.WriteString("Sabu C.Alex is working as a Software Engineer"); //Employee Element Inner Text xmlWriter.WriteEndElement(); //End of Employee Element xmlWriter.WriteStartElement("Employee"); //Started Employee Element xmlWriter.WriteStartAttribute("Name"); //Attribute "Name" xmlWriter.WriteString("Dinup Varghese"); //Attribute Value xmlWriter.WriteEndAttribute(); xmlWriter.WriteStartAttribute("Age");//Attribute "Age" xmlWriter.WriteString("28");//Attribute Value xmlWriter.WriteEndAttribute(); xmlWriter.WriteString("Dinup Varghese is working as a Software Engineer"); //Employee Element Inner Text xmlWriter.WriteEndElement(); //End of Employee Element xmlWriter.WriteEndElement(); //End of Employees Element xmlWriter.WriteEndElement(); //End of Department Element xmlWriter.WriteStartElement("Department"); //Department Element xmlWriter.WriteStartAttribute("Name"); //Attribute "Name" xmlWriter.WriteString("Accounts"); //Attribute Value xmlWriter.WriteEndAttribute(); xmlWriter.WriteStartElement("Employees"); //Started Employees Element xmlWriter.WriteStartElement("Employee"); //Started Employee Element xmlWriter.WriteStartAttribute("Name"); //Attribute "Name" xmlWriter.WriteString("Mathew"); //Attribute Value xmlWriter.WriteEndAttribute(); xmlWriter.WriteStartAttribute("Age");//Attribute "Age" xmlWriter.WriteString("28");//Attribute Value xmlWriter.WriteEndAttribute(); xmlWriter.WriteString("Mathew is working as an Account"); //Employee Element Inner Text xmlWriter.WriteEndElement(); //End of Employee Element xmlWriter.WriteStartElement("Employee"); //Started Employee Element xmlWriter.WriteStartAttribute("Name"); //Attribute "Name" xmlWriter.WriteString("Edwin"); //Attribute Value xmlWriter.WriteEndAttribute(); xmlWriter.WriteStartAttribute("Age");//Attribute "Age" xmlWriter.WriteString("28");//Attribute Value xmlWriter.WriteEndAttribute(); xmlWriter.WriteString("Edwin is working as a Junior Accountant"); //Employee Element Inner Text xmlWriter.WriteEndElement(); //End of Employee Element xmlWriter.WriteEndElement(); //End of Employees Element xmlWriter.WriteEndElement(); //End of Department Element xmlWriter.WriteEndElement(); //End of Root Element xmlWriter.WriteEndDocument(); xmlWriter.Flush(); xmlWriter.Close(); }
|
| Author: Ratheesh 18 Jul 2008 | Member Level: Gold | Rating: Points: 1 |
Choose File | Close All from the file menu to be sure that you have completely closed the package you just finished installing. Now create a new application and drop down a TButton, TMemo and TListBox on a Delphi form.
Drop down both a TXmlToDomParser object and a TDomImplementation object on your form. You can, needless to say, find both of these objects on the XML page that you just created. Now you need to link these two components together so they can work with one another. To do this use the mouse to set the DomImpl property of the TXmlToDomParser to the TDomImplementation object that resides on your form.
Create an OnClick handler for your button and enter the following lines of code in it:
procedure TForm1.Run1Click(Sender: TObject); var Doc: TDOMDocument; List: TDomNodeList; Len, i: Integer; S: string; begin Memo1.Clear; ListBox1.Items.Clear; Doc := XmlToDomParser1.FileToDom(GetStartDir + 'sam1.html'); Len := DomImplementation1.documents.Length; for i := 0 to Len - 1 do begin S := DomImplementation1.Documents.Item(i).ChildNodes.Item(i).Code; Memo1.Lines.Add(S); end;
List := Doc.DocumentElement.GetElementsByTagName('CD'); for i := 0 To List.length -1 do ListBox1.Items.Add(List.item(i).Code); end;
This code uses both of the components you just installed to iterate through an XML file and display its contents. The code actually parses the code in two different ways, both of which should be of some interest to readers of this document. The two examples I've chosen will hopefully act as starting points from which you can branch out into a more detailed exploration of this technology.
In the third line, you retrieve an instance of the TDomDocument class from TXmlToDomParser component. Needless to say, TDomDocument represents an entire XML document. I use the TXmlToDomParser component that you just installed to load an XML file. Here is a sample of a very simple XML file you can work with if you want:
<HTML> <HEAD> <TITLE>Sample XML File</TITLE> </HEAD> <BODY> <P>Right under this line I insert an XML data island.</P> <XML ID="CDXML"> <CDS> <CD>Two Against Nature</CD> <CD>Giant Steps</CD> <CD>Round About Midnight</CD> <CD>Imaginary Day</CD> </CDS> </XML> </BODY> </HTML> Just save this file to disk as Sam1.xml or Sam1.html. Then you can load it into the document you just created by writing something like this:
Doc := XmlToDomParser1.FileToDom('c:\temp\sam1.xml'); Of course, on your system, there might be a different path to the file than the one I show here.
The next thing you want to find out is how many nodes there are at the top level of your document. The nodes of the document are arranged hierarchically, so that as you dig into the document you find nodes buried within nodes. For instance, the <CD> node is buried within the <CDS> node. But at first, all we want is the top level nodes. To get them, write the following line of code:
Len := DomImplementation1.documents.Length; Now you want to iterate through these nodes and see what is in them. To get at the content of the nodes, write the following code:
var S: string; begin ... // Code ommitted heren for i := 0 to Len - 1 do begin S := DomImplementation1.Documents.Item(i).ChildNodes.Item(i).Code; Memo1.Lines.Add(S); end; ... // Code ommitted here. end; I've changed the code slightly here from the version I showed you earlier in this paper. The change was implemented to make the code easier for you to read.
The output from this part of the program should look something like this:
<HEAD> <TITLE>Sample XML File</TITLE> </HEAD> <BODY> <P>Right under this line I insert an XML data island.</P> <XML ID="CDXML"> <CDS> <CD>Two Against Nature</CD> <CD>Giant Steps</CD> <CD>Round About Midnight</CD> <CD>Imaginary Day</CD> </CDS> </XML> </BODY>
|
| Author: Gaurav Agrawal 18 Jul 2008 | Member Level: Diamond | Rating: Points: 6 |
void CreateXmlFile(String xmlFilePath) { XmlTextWriter xmlWriter = new XmlTextWriter(xmlFilePath, Encoding.UTF8); xmlWriter.WriteStartDocument(true); xmlWriter.WriteStartElement("Departments"); //Root Element xmlWriter.WriteStartElement("Department"); //Department Element xmlWriter.WriteStartAttribute("Name"); //Attribute "Name" xmlWriter.WriteString("Development"); //Attribute Value xmlWriter.WriteEndAttribute(); xmlWriter.WriteStartElement("Employees"); //Started Employees Element xmlWriter.WriteStartElement("Employee"); //Started Employee Element xmlWriter.WriteStartAttribute("Name"); //Attribute "Name" xmlWriter.WriteString("Sabu C.Alex"); //Attribute Value
xmlWriter.WriteEndAttribute(); xmlWriter.WriteStartAttribute("Age");//Attribute "Age" xmlWriter.WriteString("28");//Attribute Value xmlWriter.WriteEndAttribute(); xmlWriter.WriteString("Sabu C.Alex is working as a Software Engineer"); //Employee Element Inner Text xmlWriter.WriteEndElement(); //End of Employee Element xmlWriter.WriteStartElement("Employee"); //Started Employee Element xmlWriter.WriteStartAttribute("Name"); //Attribute "Name" xmlWriter.WriteString("Dinup Varghese"); //Attribute Value xmlWriter.WriteEndAttribute(); xmlWriter.WriteStartAttribute("Age");//Attribute "Age" xmlWriter.WriteString("28");//Attribute Value xmlWriter.WriteEndAttribute(); xmlWriter.WriteString("Dinup Varghese is working as a Software Engineer"); //Employee Element Inner Text xmlWriter.WriteEndElement(); //End of Employee Element xmlWriter.WriteEndElement(); //End of Employees Element xmlWriter.WriteEndElement(); //End of Department Element xmlWriter.WriteEndElement(); //End of Root Element xmlWriter.WriteEndDocument(); xmlWriter.Flush(); xmlWriter.Close(); }
|