Home » xmlbeans-2.5.0-src » org.apache.xmlbeans.samples » anytype » [javadoc | source]
public class: Any [javadoc | source]
A sample that illustrates various ways to manipulate XML whose schema defines elements as type xs:any. Unlike its treatment of other schema types, XMLBeans does not generate accessors for the xs:any particle when compiling schema. Instead, your code handles instances of this type through any of several alternative means, including XPath queries, the selectChildren method, XmlCursor instances and the DOM API. This samples illustrates these alternative approaches.
Method from org.apache.xmlbeans.samples.anytype.Any Summary:
buildDocFromScratch,   editExistingDocWithDOM,   editExistingDocWithSelectChildren,   editExistingDocWithSelectPath,   main,   parseXml,   printErrors,   validateXml
Methods from java.lang.Object:
clone,   equals,   finalize,   getClass,   hashCode,   notify,   notifyAll,   toString,   wait,   wait,   wait
Method from org.apache.xmlbeans.samples.anytype.Any Detail:
 public boolean buildDocFromScratch() 
    Creates a new document from scratch. This method illustrates how you can use XmlCursor instances to build XML that is defined in schema as xs:any.
 public boolean editExistingDocWithDOM(RootDocument rootDoc) 
    Adds a new element between the first and second children of the element. This method illustrates how you can use DOM methods to retrieve and edit elements whose type is defined as xs:any in schema.
 public boolean editExistingDocWithSelectChildren(RootDocument rootDoc) 
    Replaces the element with an element in the incoming XML. This method illustrates how you can use the XmlCursor.selectChildren method to retrieve child elements whose type is defined as xs:any in schema.
 public boolean editExistingDocWithSelectPath(RootDocument rootDoc) 
    Edits incoming XML to make the following changes: replace with its child; add a new element as the second child of . This method illustrates how you can use the selectPath method to find an element defined as xs:any in schema, then use XmlCursor instances to edit the XML.
 public static  void main(String[] args) 
    Receives XML instance, executing methods that edit the received instance or create a new one.
 public XmlObject 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 static  void printErrors(ArrayList validationErrors) 
    Receives the collection containing errors found during validation and print the errors to the console.
 public static 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.