|
|
|
|
|
- Overview
- Example for reading an XML document
- Example for creating an XML document
- Example for modifying an XML document
Usage example of the XML types
The following examples is used to handle an XML document via the xmlDocument and cmlNode variables, ... Example for reading an XML document NameXMLFile is string NameXMLFile = fSelect("*.*", "File.xml", "Select an XML file", ... "XML file (*.xml)" + TAB + "*.xml" + CR + "All" + TAB + "*.*") Â // Checks whether the file is selected IF NameXMLFile <> "" THEN // Open the XML file xmlDoc is xmlDocument xmlDoc = XMLOpen(NameXMLFile, fromFile) // Checks whether at least one root is found (a single one is usually found) IF xmlDoc..RootNode..Occurrence < 1 THEN Info("The XML file " + NameXMLFile + " contains no root node") ELSE // Displays the details of all the roots FOR EACH ARootNode OF xmlDoc..RootNode DetailsNodeAndSubNodes(ARootNode) END END END
// Code of the DetailsNodeAndSubNodes procedure  // Summary: Displays in a trace window the tree structure of the node passed as parameter // Syntax: // DetailsNodeAndSubNodes(<anXMLNode> is xmlNode [, <nLevel> is int]) // // Parameters: // anXMLNode (xmlNode): XML node whose details must be displayed // nLevel (integer - default value=0): Current level of the node // Return value: // None PROCEDURE DetailsNodeAndSubNodes(LOCAL anXMLNode is xmlNode, nLevel is int=0)  sAttributes is string = ""  // Checks the existence of the attributes IF anXMLNode..Attribute..Occurrence > 0 THEN // Yes, therefore browse the attributes FOR EACH aAttribute OF anXMLNode..Attribute sAttributes += " " + UnicodeToAnsi(anAttribute..Name) + "=""" + ... UnicodeToAnsi(anAttribute..Value) + """" END END  // Beginning of node Trace(RepeatString("-", 2*nLevel) + "<" + UnicodeToAnsi(anXMLNode..Name) + sAttributes + "> " + ... UnicodeToAnsi(anXMLNode..Text))  // Browse the sub-nodes FOR EACH anXMLSubNode OF anXMLNode // The name of the sub-node is not empty? // (caution, everything is in Unicode with the XML variables) IF NOT UnicodeToAnsi(anXMLSubNode..Name) ~= "" THEN // Sub-nodes DetailNodeAndSubNodes(anXMLSubNode, nLevel+1) // End of sub-node END END // End of node Trace(RepeatString("-", 2*nLevel) + "</" + anXMLNode..Name + ">")
Example for creating an XML document // Create an XML variable xmlInvoice is xmlDocument,description="Model_Invoice" // Note: ,description="Model_Invoice" is optional. // This notation gives access to the assisted input if an example XML file was imported  xmlANode is xmlNode  // Create the root (because the automatic creation is done on one level only) xmlANode..Name = "LIST_OF_INVOICES" Add(xmlInvoice, xmlANode)  // ----Create a new invoice xmlANode..Name = "INVOICE" Add(xmlInvoice.LIST_OF_INVOICES, xmlANode)  // Create the elements of the invoice xmlInvoice.LIST_OF_INVOICES.INVOICE[1].Number = 1 xmlInvoice.LIST_OF_INVOICES.INVOICE[1].TOTAL = 172.6 xmlInvoice.LIST_OF_INVOICES.INVOICE[1].VAT = 19.6  // Create a new invoice line xmlANode..Name = "INVOICE_LINE" Add(xmlInvoice.LIST_OF_INVOICES.INVOICE[1], xmlANode) xmlInvoice.LIST_OF_INVOICES.INVOICE[1].INVOICE_LINE[1].Description = ... "Description line 1 invoice 1" xmlInvoice.LIST_OF_INVOICES.INVOICE[1].INVOICE_LINE[1].AMOUNT = 84.25  // Create a new invoice line xmlANode..Name = "INVOICE_LINE" Add(xmlInvoice.LIST_OF_INVOICES.INVOICE[1], xmlANode) xmlInvoice.LIST_OF_INVOICES.INVOICE[1].INVOICE_LINE[2].Description = ... "Description line 2 invoice 1" xmlInvoice.LIST_OF_INVOICES.INVOICE[1].INVOICE_LINE[2].AMOUNT = 88.35  //----Create a new invoice xmlANode..Name = "INVOICE" Add(xmlInvoice.LIST_OF_INVOICES, xmlANode)  // Create the elements of the invoice xmlInvoice.LIST_OF_INVOICES.INVOICE[2].Number = 2 xmlInvoice.LIST_OF_INVOICES.INVOICE[2].TOTAL = 225.65 xmlInvoice.LIST_OF_INVOICES.INVOICE[2].VAT = 5.5  // --Create a new invoice line xmlANode..Name = "INVOICE_LINE" Add(xmlInvoice.LIST_OF_INVOICES.INVOICE[2], xmlANode) xmlInvoice.LIST_OF_INVOICES.INVOICE[2].INVOICE_LINE[1].Description = ... "Description line 1 invoice 2" xmlInvoice.LIST_OF_INVOICES.INVOICE[2].INVOICE_LINE[1].AMOUNT = 52.35  // --Create a new invoice line xmlANode..Name = "INVOICE_LINE" Add(xmlInvoice.LIST_OF_INVOICES.INVOICE[2], xmlANode) xmlInvoice.LIST_OF_INVOICES.INVOICE[2].INVOICE_LINE[2].Description = ... "Description line 2 invoice 2" xmlInvoice.LIST_OF_INVOICES.INVOICE[2].INVOICE_LINE[2].AMOUNT = 14.45  // --Create a new invoice line xmlANode..Name="INVOICE_LINE" Add(xmlInvoice.LIST_OF_INVOICES.INVOICE[2], xmlANode) xmlInvoice.LIST_OF_INVOICES.INVOICE[2].INVOICE_LINE[3].Description = ... "Description line 3 invoice 2" xmlInvoice.LIST_OF_INVOICES.INVOICE[2].INVOICE_LINE[3].AMOUNT = 158.85  // Result string Info(XMLBuildString(xmlInvoice)) // or XMLSave(xmlInvoice, SysDir(srMyDocuments) + ["\"] + ... //       "List_Invoices.xml", XMLDocumentDefault)
Example for modifying an XML document // Modify the XML document // Load the XML content MyXMLDoc is xmlDocument,description="NEWS" MyXMLDoc = XMLOpen(gsPathXMLFile, fromFile) IF ErrorOccurred = True THEN RESULT "Error detected when opening the XML: " + ErrorInfo() ELSE Trace("XML file currently processed: " + gsPathXMLFile) END Â NodeRSS is xmlNode,description="NEWS.rss.channel.item" FOR EACH NodeRSS OF Â MyXMLDoc.rss.channel ON item IF NodeRSS.title = sFormer_Title THEN Trace("The news was found, we are going to modify it") NodeRSS.title = sNew_Title NodeRSS.description = sNew_Comment NodeRSS.link = sNew_Url END END XMLSave(MyXMLDoc, gsPathXMLFile) Â // Everything is correct RETURN ""
This page is also available for…
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|