|
|
|
|
|
XML management: Use example
This application example manages an XML file named "Orders.xml". This file contains data regarding an order. This example shows how to handle XML files using character strings. Sample content of "orders.xml": The following WLanguage code is used to: - create an XML document from the "Orders.XML" file:
// Retrieve the XML content of "orders.xml" file MyXMLSource is string = fLoadText("C:\MyOrders\Orders.xml") // Create the XML document XMLDocument("XMLOrd", MyXMLSource) IF ErrorOccurred = True THEN Error("Error while creating the XML document") RETURN END
- browse the XML document and retrieve the content of its elements:
// Positions on the "" element XMLFirst("XMLOrd") // Moves one level down and positions on the "" element XMLChild("XMLOrd") // Retrieves the order number OrdNum is int = Val(XMLData("XMLOrd")) Â // contains 1 // Positions on the next element XMLNext("XMLOrd") // Retrieves the order date OrdDate is string = XMLData("XMLOrd") Â // "11/20/2002"
- find an element in the XML document and retrieve its content:
// Find the product code XMLFind("XMLCde", "productcode", XMLTag + XMLCurrentLevel + XMLChildItem) IF XMLFound ("XMLCde") = True THEN PC is string = XMLData("XMLOrd") // contains "CDR-1080" END
- add elements into the XML document:
// Add a new order line XMLParent("XMLOrd") XMLParent("XMLOrd") XMLAddChild("XMLOrd", "orderline", "", True) XMLAddAttribute("XMLOrd", "number", "2") Â // Add the "productcode" tag XMLAddChild("XMLOrd", "productcode", "sro2125") Â // Add the "description" tag XMLAddChild("XMLOrd", "description", "optical mouse") Â // Add the "quantity" tag XMLAddChild("XMLOrd", "quantity", "15")
- save the modifications made to the XML file:
// Format the content of the XML document XMLFile is string = XMLBuildString("XMLOrd") Â // Save the XML file fSaveText("C:\MyOrders\Orders.xml", XMLFile)
- close the XML document and display the result:
//Close the document XMLClose("XMLOrd") Â // Display the data Info("Order Number: " + OrdNum, "Order date: " + OrdDate, "Product code: " + PC)
This page is also available for…
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|