Create items and sequences using the XItemFactory
Use XItemFactory to create new items and sequences of items of different types.
Tasks
- Use the getItemFactory method in the XFactory class to create instances of XItemFactory and call the appropriate item method to create an item of a specific type.
An item itself can be a node or an atomic value such as an integer, string, or boolean.
The following is an example of using the XItemFactory to create new items of different types.
// Create an XFactory XFactory factory = XFactory.newInstance(); // Create an XItemFactory XItemFactory itemFactory = factory.getItemFactory(); // Create a new atomic item of a type which is the default mapping of the xs:string built-in type to java.lang.String XItemView stringItem = itemFactory.item("Lets see"); // Create a new atomic item of type int XItemView intItem = itemFactory.item(3, XTypeConstants.INT_QNAME); // Create a new atomic item of type boolean boolean boolValue = false; XItemView booleanItem = itemFactory.item(boolValue, XTypeConstants.BOOLEAN_QNAME); // Create Node type DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); // Parse the input file Document doc = db.parse(INPUT_File); Node node = doc.getFirstChild(); XItemView item =itemFactory.item(node) // Create a new item of complex type from a Source StreamSource source = new StreamSource(INPUT_File); XItemView complexItem = itemFactory.item(source);As shown in the example, we can use item method with one argument as the value of the method and the type is evaluated based on the mapping rules between built-in types and Java types.
- Use the getItemFactory method in the XFactory class to create instances of XItemFactory and call the appropriate sequence method to create an XSequenceCursor that represents a sequence of items providing cursor access to the items in the sequence.
The following is an example of using XItemFactory to create a homogeneous sequence.
// Create an XFactory XFactory factory = XFactory.newInstance(); // Create an XItemXFactory XItemFactory itemFactory =xfactory.getItemFactory(); //Create a sequence of int values XSequenceCursor intSeq = xfactory.sequence(new int[]{1,2,3}); //Create a sequence of String values XSequenceCursor stringSeq = xfactory.sequence(new String[]{"This", "is", "a", "test"},XTypeConstants.STRING_QNAME );The following is an example of using the XItemFactory to create a sequence of items of different types.
// Create an XFactory XFactory factory = XFactory.newInstance(); // Create an XItemXFactory XItemFactory itemFactory = factory.getItemFactory(); //Create an Array of the newly created items XItemView[] items = new XItemView[2]; items[0] =itemFactory.item(boolValue, XTypeConstants.BOOLEAN_QNAME); items[1] = itemFactory.item(intValue, XTypeConstants.INT_QNAME); // Create a sequence of items XSequenceCursor seq = itemFactory.sequence(items);
Mapping XML types to Java types Navigating with XSequenceCursor