XML Kunwar Singh
Overview of Presentation What is XML? Why do we need it? Importance of XML XML Applications XML Market XML Sample Applications Sample Output References
What is XML? XML is the Extensible Markup Language Not a fixed format like HTML XML is actually a `metalanguage' —a language for describing other languages
Why do we need it ? To define document types Easy to author and manage SGML-defined documents Easy to transmit and share them across the Web XML has been designed for ease of implementation, and for interoperability with both SGML and HTML
Importance of XML It removes two constraints which were holding back Web developments: dependence on a single, inflexible document type the complexity of full SGML, whose syntax allows many powerful but hard-to-program options
What does an XML document look like inside? <?xml version="1.0" standalone="yes"?><conversation> <greeting>Hello, world!</greeting> <response>Stop the planet, I want to get off!</response></conversation>
Applications Data interchange format in B2B Web applications such as e-commerce, supply-chain management, workflow, and application integration Media-independent publishing XML can be used to create customized views into data
Why XML in Pharmaceutical companies? XML is open Human-readable Self-explaining Highly-structured Extensible
Sample Application Code <?xml version="1.0" encoding="utf-8"?> <addressbook> <contacts id="Contacts"> <contact id="Alex"> <email id="popmail"> someone@some_pop_mail.net</email> <city>Edinburgh</city> <country>United Kingdom</country> </contact> <contact id="Rebekah"> <email id="webmail"> someone@some_web_mail.net</email> <city>Papakura</city> <country>New Zealand</country> </contact> <contact id="Justin"> <email id="webmail"> someone_else@some_web_mail.com</email> <city>Muriwai</city> <country>New Zealand</country> </contact> </contacts> </addressbook>
Iterate Recursively [C#] private void populateTreeControl( System.Xml.XmlNode document, System.Windows.Forms.TreeNodeCollection nodes) { foreach (System.Xml.XmlNode node in document.ChildNodes) { // If the element has a value, display it; // otherwise display the first attribute // (if there is one) or the element name // (if there isn't) string text = (node.Value != null ? node.Value : (node.Attributes != null && node.Attributes.Count > 0) ? node.Attributes[0].Value : node.Name); TreeNode new_child = new TreeNode(text); nodes.Add(new_child); populateTreeControl(node, new_child.Nodes); } }
Iterate Recursively cont… [VB] Private Sub populateTreeControl( _ ByVal document As System.Xml.XmlNode, _ ByVal nodes As _ System.Windows.Forms.TreeNodeCollection) Dim node As System.Xml.XmlNode For Each node In document.ChildNodes ' If the element has a value, display it; ' otherwise display the first attribute ' (if there is one) or the element name ' (if there isn't) Dim [text] As String If node.Value <> Nothing Then [text] = node.Value Else If Not node.Attributes Is Nothing And _ node.Attributes.Count > 0 Then [text] = node.Attributes(0).Value Else [text] = node.Name End If End If Dim new_child As New TreeNode([text]) nodes.Add(new_child) populateTreeControl(node, new_child.Nodes) Next node End Sub
Create a new Windows Forms application, drop a TreeView control onto the form [C#] System.Xml.XmlDocument document = new System.Xml.XmlDataDocument(); document.Load("../../contacts.xml"); populateTreeControl(document.DocumentElement, treeView1.Nodes); [VB] Dim document As New System.Xml.XmlDataDocument() document.Load("../contacts.xml") populateTreeControl(document.DocumentElement, _ TreeView1.Nodes)
Define what structures the hierarchical view can or cannot manipulate within a given document [C#] private void populateTreeControl(System.Xml.XmlNode document, System.Windows.Forms.TreeNodeCollection nodes) { foreach (System.Xml.XmlNode node in document.ChildNodes) { System.Xml.XmlNode expr = node.SelectSingleNode(xpath_filter); if (expr != null) { TreeNode new_child = new TreeNode(expr.Value); nodes.Add(new_child); populateTreeControl(node, new_child.Nodes); } } }
Define what structures the hierarchical view can or cannot manipulate within a given document [VB] Private Sub populateTreeControl( _ ByVal document As System.Xml.XmlNode, _ ByVal nodes As System.Windows.Forms.TreeNodeCollection) Dim node As System.Xml.XmlNode For Each node In document.ChildNodes Dim expr As System.Xml.XmlNode = _ node.SelectSingleNode(xpath_filter) If Not (expr Is Nothing) Then Dim new_child As New TreeNode(expr.Value) nodes.Add(new_child) populateTreeControl(node, new_child.Nodes) End If Next End Sub
Implementing drag and drop operations [C] private void XmlTreeView_DragEnter(object sender, System.Windows.Forms.DragEventArgs e) { // Allow the user to drag tree nodes within a // tree if (e.Data.GetDataPresent( "System.Windows.Forms.TreeNode", true )) { e.Effect = DragDropEffects.Move; drag_drop_active = true; } else e.Effect = DragDropEffects.None; }
Implementing drag and drop operations cont… [VB] Private Sub XmlTreeView_DragEnter( _ ByVal sender As Object, _ ByVal e As System.Windows.Forms.DragEventArgs) _ Handles MyBase.DragEnter ' Allow the user to drag tree nodes within a tree If e.Data.GetDataPresent( _ "System.Windows.Forms.TreeNode", True) Then e.Effect = DragDropEffects.Move drag_drop_active = True Else e.Effect = DragDropEffects.None End If End Sub
Implementing Delete, Rename, and Insert Operations [Delete] [C]string xpath_query = buildXPathQuery(this.SelectedNode); System.Xml.XmlNode node = xml_document.DocumentElement.SelectSingleNode( xpath_query); node.ParentNode.RemoveChild(node); SelectedNode.Remove(); [VB] Dim xpath_query As String = _ buildXPathQuery(Me.SelectedNode) Dim node As System.Xml.XmlNode = _ xml_document.DocumentElement.SelectSingleNode( _ xpath_query) node.ParentNode.RemoveChild(node) SelectedNode.Remove()
Renaming a folder with XPath filter private void XmlTreeView_AfterLabelEdit(object sender, System.Windows.Forms.NodeLabelEditEventArgs e) { string xpath_query = buildXPathQuery(e.Node); System.Xml.XmlNode node = xml_document.DocumentElement.SelectSingleNode( xpath_query); System.Xml.XmlNode label = node.SelectSingleNode(xpath_filter); label.Value = e.Label; } [VB] Private Sub XmlTreeView_AfterLabelEdit( _ ByVal sender As Object, _ ByVal e As System.Windows.Forms.NodeLabelEditEventArgs) _ Handles MyBase.AfterLabelEdit Dim xpath_query As String = buildXPathQuery(e.Node) Dim node As System.Xml.XmlNode = _ xml_document.DocumentElement.SelectSingleNode( _ xpath_query) Dim label As System.Xml.XmlNode = node.SelectSingleNode(xpath_filter) label.Value = e.Label End Sub
Insert a Folder System.Xml.XmlDocument insert_fragment = new System.Xml.XmlDocument(); insert_fragment.LoadXml( "<product id='New Item'>" + "<description/><ordercode/><price/>" + "<image/></product>"); // The TreeView uses XmlInsertionNode to add // a new folder to the tree's underlying XML // document on request xmlTreeView1.XmlInsertionNode = insert_fragment.DocumentElement; [VB] Dim insert_fragment As New System.Xml.XmlDocument() insert_fragment.LoadXml(" & _ "<product id='New Item'>" & _ "<description/><ordercode/>"<price/>" & _ "<image/></product>") xmlTreeView1.XmlInsertionNode = _ insert_fragment.DocumentElement
Ensure the folder is defined in such a way that the filter query identifies it as a folder… [C#] // First you need to clone the node template, and // import it, because it originates from a different // document System.Xml.XmlNode copy_node = new_node.Clone(); System.Xml.XmlNode insert_node = xml_document.ImportNode(copy_node, true); // Next locate which node should be its parent, and // insert it string xpath_query = buildXPathQuery(this.SelectedNode); System.Xml.XmlNode node = xml_document.DocumentElement.SelectSingleNode( xpath_query); node.AppendChild(insert_node); // Finally, apply the xpath filter to determine what // to display System.Xml.XmlNode expr = insert_node.SelectSingleNode(xpath_filter); System.Windows.Forms.TreeNode new_child = SelectedNode.Nodes.Add(expr.Value); populateTreeControl(insert_node, new_child.Nodes); // Select the node, to force the tree to expand SelectedNode = new_child; // And start editing the new folder name suppress_label_edit = false; new_child.BeginEdit();
Ensure the folder is defined in such a way that the filter query identifies it as a folder… [VB] ' First you need to clone the node template, and ' import it, because it originates from a different ' document. Dim copy_node As System.Xml.XmlNode = new_node.Clone() Dim insert_node As System.Xml.XmlNode = _ xml_document.ImportNode(copy_node, True) ' Next locate which node should be its parent, ' and insert it Dim xpath_query As String = _ buildXPathQuery(Me.SelectedNode) Dim node As System.Xml.XmlNode = xml_document.DocumentElement.SelectSingleNode( _ xpath_query) node.AppendChild(insert_node) ' Finally, apply the xpath filter to determine what ' should be ' displayed Dim expr As System.Xml.XmlNode = _ insert_node.SelectSingleNode(xpath_filter) Dim new_child As System.Windows.Forms.TreeNode = _ SelectedNode.Nodes.Add(expr.Value) populateTreeControl(insert_node, new_child.Nodes) ' Select the node, to force the tree to expand SelectedNode = new_child ' And start editing the new folder name suppress_label_edit = False new_child.BeginEdit()
Load the XML document from a file xmlDocument = new System.Xml.XmlDataDocument(); xmlDocument.Load(strXmlDocument); // After setting the path filter, load the // document into the TreeView xmlTreeView1.XPathFilter = "attribute::id"; xmlTreeView1.Load(xmlDocument); // Defining XmlInsertionNode allows creation of new // nodes within the TreeView System.Xml.XmlDocument insert_fragment = new System.Xml.XmlDocument(); insert_fragment.LoadXml("<product id='New Item'><description/><ordercode/><price/> <image/></product>"); xmlTreeView1.XmlInsertionNode = insert_fragment.DocumentElement;
Load the XML document from a file [VB] ' Load the XML document from a file xmlDocument = New System.Xml.XmlDataDocument() xmlDocument.Load(strXmlDocument) ' After setting the path filter, load the ' document into the TreeView xmlTreeView1.XPathFilter = "attribute::id" xmlTreeView1.Load(xmlDocument) ' Defining XmlInsertionNode allows creation of new ' nodes within the TreeView Dim insert_fragment As New System.Xml.XmlDocument() insert_fragment.LoadXml("<product id='New " & _ "Item'><description/><ordercode/><price/>" & _ "<image/></product>") xmlTreeView1.XmlInsertionNode = insert_fragment.DocumentElement
Final Catalog- OutPut- Hierarchial Tree View
Final catalog- Detailed view
Some More XML Examples http://www.w3schools.com/xml/tryit.asp?filename=cd_list http://www.w3schools.com/xml/tryit.asp?filename=cd_navigate http://www.w3schools.com/xml/tryit.asp?filename=cd_application
References and Sources http://www.projectcool.com/developer/xmlz www.Msdn.microsoft.com/workshop/xml/default www.Developer.netscape.com/tech/metadata/index.html www.oasis-open.org www.ucc.ie/xml/ www.w3schools.com
Questions/ Comments ???????????????????? ?