Home » xmlbeans-2.5.0-src » org.apache.xmlbeans.samples » cursor » [javadoc | source]
org.apache.xmlbeans.samples.cursor
public class: MixedContent [javadoc | source]
java.lang.Object
   org.apache.xmlbeans.samples.cursor.MixedContent

This sample illustrates how you can use an XML cursor to manipulate the content of an element. Even though working with strongly-typed XML (in which you are accessing the XML through an API generated from schema) provides easy access for getting and setting the entire value of an element or attribute, it does not easily provide finer grained access to an element's content. This sample shows how you can use an XML cursor to "dive into" an element's content, manipulating it on a character-by- character level.

The code in this sample is designed to look at the description of each item in an inventory list, creating a link wherever the description contains a reference to another item in the inventory list. This alters the <description> element so that it contains a mix of text and link elements. Such an element is said to have "mixed content."

This sample uses the schema defined in inventory.xsd.
Method from org.apache.xmlbeans.samples.cursor.MixedContent Summary:
linkItems,   main,   parseXml,   validateXml
Methods from java.lang.Object:
clone,   equals,   finalize,   getClass,   hashCode,   notify,   notifyAll,   toString,   wait,   wait,   wait
Method from org.apache.xmlbeans.samples.cursor.MixedContent Detail:
 public InventoryDocument linkItems(InventoryDocument inventoryDoc) 

    Creates "links" between items in an inventory list by inserting a <link> element for each linked item. An XmlCursor instance passes through each <description> element, looking for text matching the name of an item.

 public static  void main(String[] args) 
    Receives an inventory XML instance and rewrites it so that items listed in the inventory point to one another via <link> elements.
 public InventoryDocument parseXml(String xmlFilePath) 

    Creates a File from the XML path provided in main arguments, then parses the file's contents into a type generated from schema.

    Note that this work might have been done in main. Isolating it here makes the code separately available from outside this class.

 public boolean validateXml(XmlObject xml) 

    Validates the XML, printing error messages when the XML is invalid. Note that this method will properly validate any instance of a compiled schema type because all of these types extend XmlObject.

    Note that in actual practice, you'll probably want to use an assertion when validating if you want to ensure that your code doesn't pass along invalid XML. This sample prints the generated XML whether or not it's valid so that you can see the result in both cases.