How to Map Objects to XML
Extensible markup language, or XML, is a programming language that can best be described as a markup language that can be customized to use other markup languages, such as HTML. XML files are custom created to meet specific application development needs. If you're using an object-oriented programming language such as C#, you may need to know how to map objects from your C# application to XML files that you've written.
Instructions
-
-
1
Create two classes for the XML objects to map to by writing the following lines of code, replacing "Product" and "products" with the name of the XML field from which you need to map.
public class Order {
[ChoiceType("com.foo.Product")]
public var products:Array;
}
public class Product {
(Required)
public var XML_example_field:String;
}
-
2
Create a mapper that will be responsible for mapping the two class structures together by using the following lines of code:
var mapper :XMLObjectMapper = XMLObjectMappings
.forUnqualifiedElements()
.withoutRootElement(Order)
.mappedClasses(Product)
.build();
-
-
3
Map objects to the XML files using the following lines of code:
var order:Order = ...;
var xml:XML = mapper.mapToXML(order);
-
1