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 Detail: |
public boolean buildDocFromScratch() {
// Start by creating a < root > element that will contain
// the children built by this method.
RootDocument rootDoc = RootDocument.Factory.newInstance();
RootDocument.Root root = rootDoc.addNewRoot();
// Add the first element, < stringelement >.
root.setStringelement("some text");
// Create an XmlObject in which to build the second
// element in the sequence, < anyfoo >. Here, the
// XmlObject instance is simply a kind of incubator
// for the XML. Later the XML will be moved into the
// document this code is building.
XmlObject anyFoo = XmlObject.Factory.newInstance();
// Add a cursor to do the work of building the XML.
XmlCursor childCursor = anyFoo.newCursor();
childCursor.toNextToken();
// Add the element in the schema's namespace, then add
// element content.
childCursor.beginElement(new QName(m_namespaceUri, "anyfoo"));
childCursor.insertChars("some text");
// Move the cursor back to the new element's top, where
// it can grab the element's XML.
childCursor.toStartDoc();
childCursor.toNextToken();
// Move the XML into the < root > document by moving it
// from a position at one cursor to a position at
// another.
XmlCursor rootCursor = root.newCursor();
rootCursor.toEndToken();
childCursor.moveXml(rootCursor);
// Add the fourth element, < arrayofany >, by building it
// elsewhere, then moving the new XML into place under
// < root >.
Arrayofany arrayOfAny = root.addNewArrayofany();
if (buildArrayOfAny(arrayOfAny) == null)
{
return false;
}
childCursor.dispose();
rootCursor.dispose();
// Print and validate the result.
System.out.println("Output: The < root > document built from scratch.\n");
System.out.println(rootDoc + "\n");
return validateXml(rootDoc);
}
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) {
RootDocument.Root root = rootDoc.getRoot();
// Get the DOM nodes for the < arrayofany > element's children.
Node arrayOfAnyNode = root.getArrayofany().getDomNode();
// You don't have get* accessors for any of the < arrayofany >
// element's children, so use DOM to identify the first
// and second elements while looping through the child list.
NodeList childList = arrayOfAnyNode.getChildNodes();
Element firstElementChild = null;
Element secondElementChild = null;
// Find the first child element and make sure it's
// < stringelement >.
for (int i = 0; i < childList.getLength(); i++)
{
Node node = childList.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE)
{
if (node.getLocalName().equals("stringelement"))
{
firstElementChild = (Element)node;
break;
}
}
}
if (firstElementChild == null) {return false;}
// Find the second child element and make sure it's
// < someelement >.
Node node = firstElementChild.getNextSibling();
do {
if (node.getNodeType() == Node.ELEMENT_NODE)
{
if (node.getLocalName().equals("someelement"))
{
secondElementChild = (Element)node;
break;
}
}
node = node.getNextSibling();
} while (node != null);
if (secondElementChild == null) {return false;}
// Create and insert a new < bar > element.
Element fooElement =
secondElementChild.getOwnerDocument().createElementNS("http://openuri.org","bar");
Node valueNode =
fooElement.getOwnerDocument().createTextNode("some text");
fooElement.appendChild(valueNode);
arrayOfAnyNode.insertBefore(fooElement, secondElementChild);
System.out.println("Output: < arrayofany > has a new < bar > child element.\n");
System.out.println(rootDoc + "\n");
return validateXml(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) {
RootDocument.Root root = rootDoc.getRoot();
// Select the < anyfoo > children of < root >.
XmlObject[] stringElements =
root.selectChildren(new QName(m_namespaceUri, "anyfoo"));
// If the element is there, replace it with another element.
if (stringElements.length > 0)
{
XmlCursor editCursor = stringElements[0].newCursor();
editCursor.removeXml();
editCursor.beginElement(new QName(m_namespaceUri, "anybar"));
editCursor.insertChars("some other text");
editCursor.dispose();
}
System.out.println("Output: The < anyfoo > element has been replaced\n" +
"by an < anybar > element.\n");
System.out.println(rootDoc + "\n");
return validateXml(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) {
String namespaceDecl = "declare namespace any='" +
m_namespaceUri + "'; ";
XmlCursor selectionCursor = rootDoc.getRoot().getArrayofany().newCursor();
// Save the cursor's position for later, then use XPath
// and cursor movement to position the cursor at
// the < stringlist > element.
selectionCursor.push();
selectionCursor.selectPath(namespaceDecl +
"$this//any:someelement/any:stringlist");
selectionCursor.toNextSelection();
// Create a new cursor and move it to the selection
// cursor's < someelement > parent. Moving the
// < stringlist > element to this position, displacing
// the < someelement > downward, then removing the
// < someelement > XML effectively replaces < someelement >
// with < stringlist >.
XmlCursor editCursor = selectionCursor.newCursor();
editCursor.toParent();
selectionCursor.moveXml(editCursor);
editCursor.removeXml();
editCursor.dispose();
// Return the cursor to the < arrayofany > element so you
// can do more editing. Then move the cursor to the second
// child and insert a new element as second child.
selectionCursor.pop();
selectionCursor.toFirstChild();
selectionCursor.toNextSibling();
selectionCursor.beginElement("foo", "http://openuri.org");
selectionCursor.insertChars("some text");
selectionCursor.dispose();
System.out.println("Output: < stringlist > has been promoted to replace \n" +
"< someelement >, and there's a new < foo > element.\n");
System.out.println(rootDoc + "\n");
return validateXml(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) {
Any thisSample = new Any();
System.out.println("Running Any.buildDocFromScratch\n");
thisSample.buildDocFromScratch();
RootDocument rootDoc = (RootDocument)thisSample.parseXml(args[0]);
System.out.println("Running Any.editExistingDocWithSelectChildren\n");
thisSample.editExistingDocWithSelectChildren(rootDoc);
System.out.println("Running Any.editExistingDocWithDOM\n");
thisSample.editExistingDocWithDOM(rootDoc);
System.out.println("Running Any.editExistingDocWithSelectPath\n");
thisSample.editExistingDocWithSelectPath(rootDoc);
}
Receives XML instance, executing methods that
edit the received instance or create a new one. |
public XmlObject parseXml(String xmlFilePath) {
File xmlFile = new File(xmlFilePath);
XmlObject xml = null;
try
{
xml = XmlObject.Factory.parse(xmlFile);
} catch (XmlException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
return xml;
}
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) {
Iterator iter = validationErrors.iterator();
while (iter.hasNext())
{
System.out.println(" > > " + iter.next() + "\n");
}
}
Receives the collection containing errors found during
validation and print the errors to the console. |
public static boolean validateXml(XmlObject xml) {
boolean isXmlValid = false;
// A collection instance to hold validation error messages.
ArrayList validationMessages = new ArrayList();
// Validate the XML, collecting messages.
isXmlValid = xml.validate(
new XmlOptions().setErrorListener(validationMessages));
// If the XML isn't valid, print the messages.
if (!isXmlValid)
{
printErrors(validationMessages);
}
return isXmlValid;
}
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.
|