Interface XmlObject

    • Method Detail

      • schemaType

        SchemaType schemaType()
        Returns:
        The schema type for this instance. This is a permanent, unchanging property of the instance.
      • validate

        boolean validate()
        Does a deep validation of the entire subtree under the object, but does not validate the parents or siblings of the object if the object is in the interior of an xml tree.
        Returns:
        true if the contents of this object are valid accoring to schemaType().
      • validate

        boolean validate​(XmlOptions options)

        Just like validate(), but with options.

        If you wish to collect error messages and locations while validating, use the XmlOptions.setErrorListener(java.util.Collection) method. With that method, you can specify an object in which to store messages related to validation. The following is a simple example.

         // Create an XmlOptions instance and set the error listener.
         XmlOptions validateOptions = new XmlOptions();
         ArrayList errorList = new ArrayList();
         validateOptions.setErrorListener(errorList);
         
         // Validate the XML.
         boolean isValid = newEmp.validate(validateOptions);
         
         // If the XML isn't valid, loop through the listener's contents,
         // printing contained messages.
         if (!isValid)
         {
              for (int i = 0; i < errorList.size(); i++)
              {
                  XmlError error = (XmlError)errorList.get(i);
                  
                  System.out.println("\n");
                  System.out.println("Message: " + error.getMessage() + "\n");
                  System.out.println("Location of invalid XML: " + 
                      error.getCursorLocation().xmlText() + "\n");
              }
         }
         
        Parameters:
        options - An object that implements the Collection interface.
        Returns:
        true if the contents of this object are valid accoring to schemaType().
      • selectPath

        XmlObject[] selectPath​(java.lang.String path)
        Selects a path. Path can be a string or precompiled path String.

        The path must be a relative path, where "." represents the element or attribute containg this XmlObject, and it must select only other elements or attributes. If a non-element or non-attribute is selected, an unchecked exception is thrown.

        The array that is returned contains all the selected XmlObjects, within the same document, listed in document order. The actual array type of the result is inferred from the closest common base type of selected results.

        Here is an example of usage. Suppose we have a global element definition for "owner" whose type is "person":

           <schema targetNamespace="http://openuri.org/sample">
              <element name="owner" type="person"/>
              <complexType name="person">
                 [...]
              </complexType>
           </schema>
         
        and suppose "owner" tags can be scattered throughout the document. Then we can write the following code to find them all:
         import org.openuri.sample.Person;
         import org.apache.xmlbeans.*;
         [...]
           XmlObject xobj = XmlObject.Factory.parse(myFile);
           Person[] results;
           results = (Person[])xobj.selectPath(
              "declare namespace s='http://www.openuri.org/sample' " +
              ".//s:owner");
         
        Notice the way in which namespace declarations are done in XPath 2.0. Since XPath can only navigate within an XML document - it cannot construct new XML - the resulting XmlObjects all reside in the same XML document as this XmlObject itself.
        Parameters:
        path - the xpath
        Returns:
        an array of all selected XmlObjects
      • selectPath

        XmlObject[] selectPath​(java.lang.String path,
                               XmlOptions options)
        Selects a path, applying options.
        Parameters:
        path - the xpath
        options - the options used to execute the xpath
        Returns:
        an array of all selected XmlObjects
        See Also:
        selectPath(String)
      • execQuery

        XmlObject[] execQuery​(java.lang.String query)
        Executes a query. Query can be a string or precompiled query String.

        An XQuery is very similar to an XPath, except that it also permits construction of new XML. As a result, the XmlObjects that are returned from execQuery are in newly created documents, separate from the XmlObject on which the query is executed.

        Syntax and usage is otherwise similar to selectPath.

        Parameters:
        query - The XQuery expression
        Returns:
        an array of all selected XmlObjects
        See Also:
        selectPath(String)
      • changeType

        XmlObject changeType​(SchemaType newType)
        Changes the schema type associated with this data and returns a new XmlObject instance whose schemaType is the new type.

        Returns the new XmlObject if the type change was successful, the old XmlObject if no changes could be made.

        Certain type changes may be prohibited in the interior of an xml tree due to schema type system constraints (that is, due to a parent container within which the newly specified type is not permissible), but there are no constraints at the roottype changes are never prohibited at the root of an xml tree.

        If the type change is allowed, then the new XmlObject should be used rather than the old one. The old XmlObject instance and any other XmlObject instances in the subtree are permanently invalidated and should not be used. (They will return XmlValueDisconnectedException if you try to use them.) If a type change is done on the interior of an Xml tree, then xsi:type attributes are updated as needed.

        Returns:
        a new XmlObject instance whose schemaType is the new type
      • substitute

        XmlObject substitute​(javax.xml.namespace.QName newName,
                             SchemaType newType)
        Changes the schema type associated with this data using substitution groups and returns an XmlObject instance whose schemaType is the new type and container name is the new name.

        Returns the new XmlObject if the substitution was successful, the old XmlObject if no changes could be made.

        In order for the operation to succeed, several conditions must hold:

        • the container of this type must be an element
        • a global element with the name newName must exist and must be in the substition group of the containing element
        • the newType type must be consistent with the declared type of the new element

        If the type change is allowed, then the new XmlObject should be used rather than the old one. The old XmlObject instance and any other XmlObject instances in the subtree are permanently invalidated and should not be used. (They will return XmlValueDisconnectedException if you try to use them.) If necessary, xsi:type attributes are updated.

        Parameters:
        newName - the new name
        newType - the new type
        Returns:
        an XmlObject instance whose schemaType is the new type and container name is the new name
      • isNil

        boolean isNil()
        Note that in order to be nil, the value must be in an element, and the element containing the value must be marked as nillable in the schema.
        Returns:
        true if the value is nil.
      • setNil

        void setNil()
        Sets the value to nil. The element containing the value must be marked as nillable in the schema.
      • toString

        java.lang.String toString()
        The string is pretty-printed. If you want a non-pretty-printed string, or if you want to control options precisely, use the xmlText() methods.

        Note that when producing XML any object other than very root of the document, then you are guaranteed to be looking at only a fragment of XML, i.e., just the contents of an element or attribute, and and we will produce a string that starts with an <xml-fragment> tag. The XmlOptions.setSaveOuter() option on xmlText can be used to produce the actual element name above the object if you wish.

        Overrides:
        toString in class java.lang.Object
        Returns:
        a XML string for this XML object.
      • isImmutable

        boolean isImmutable()
        Immutable values do not have a position in a tree; rather, they are stand-alone simple type values. If the object is immutable, the equals() methods tests for value equality, and the object can be used as the key for a hash.
        Returns:
        true if the value is an immutable value.
      • set

        XmlObject set​(XmlObject srcObj)
        Set the value/type of this XmlObject to be a copy of the source XmlObject. Because the type of the source may be different than this target, this XmlObject may become defunct. In this case the new XmlObject is returned. If no type change happens, the same this will be returned.
      • copy

        XmlObject copy()
        Returns a deep copy of this XmlObject. The returned object has the same type as the current object, and has all the content of the XML document underneath the current object. Note that any parts of the XML document above or outside this XmlObject are not copied. Note: The result object will be in the same synchronization domain as the source, and additional synchronization is required for concurent access. To use a different synchronization domain use setCopyUseNewSynchronizationDomain option with copy(XmlOptions) method.
        See Also:
        copy(XmlOptions), XmlOptions.setCopyUseNewSynchronizationDomain(boolean)
      • copy

        XmlObject copy​(XmlOptions options)
        Returns a deep copy of this XmlObject. The returned object has the same type as the current object, and has all the content of the XML document underneath the current object. Note that any parts of the XML document above or outside this XmlObject are not copied. Note: The result object will be in the same synchronization domain as the source, and additional synchronization is required for concurent access. To use a different synchronization domain use setCopyUseNewSynchronizationDomain option when creating the original XmlObject.
        See Also:
        XmlOptions.setCopyUseNewSynchronizationDomain(boolean)
      • valueEquals

        boolean valueEquals​(XmlObject obj)
        True if the xml values are equal. Two different objects (which are distinguished by equals(obj) == false) may of course have equal values (valueEquals(obj) == true).

        Usually this method can be treated as an ordinary equvalence relation, but actually it is not is not transitive. Here is a precise specification:

        There are two categories of XML object: objects with a known instance type, and objects whose only known type is one of the ur-types (either AnyType or AnySimpleType). The first category is compared in terms of logical value spaces, and the second category is compared lexically.

        Within each of these two categories, valueEquals is a well-behaved equivalence relation. However, when comparing an object of known type with an object with ur-type, the comparison is done by attempting to convert the lexical form of the ur-typed object into the other type, and then comparing the results. Ur-typed objects are therefore treated as lexical wildcards and may be equal to objects in different value spaces, even though the objects in different value spaces are not equal to each other.

        For example, the anySimpleType value "1" will compare as an equalValue to the string "1", the float value "1.0", the double value "1.0", the decimal "1", and the GYear "1", even though all these objects will compare unequal to each other since they lie in different value spaces. Note: as of XMLBeans 2.2.1 only implemented for simple type values.

      • valueHashCode

        int valueHashCode()
      • compareTo

        int compareTo​(java.lang.Object obj)
        Impelements the Comparable interface by comparing two simple xml values based on their standard XML schema ordering. Throws a ClassCastException if no standard ordering applies, or if the two values are incomparable within a partial order.
      • compareValue

        int compareValue​(XmlObject obj)
        This comparison method is similar to compareTo, but rather than throwing a ClassCastException when two values are incomparable, it returns the number 2. The result codes are -1 if this object is less than obj, 1 if this object is greater than obj, zero if the objects are equal, and 2 if the objects are incomparable.
      • selectChildren

        XmlObject[] selectChildren​(javax.xml.namespace.QName elementName)
        Selects the contents of the children elements with the given name.
        Parameters:
        elementName - The name of the elements to be selected.
        Returns:
        Returns the contents of the selected elements.
      • selectChildren

        XmlObject[] selectChildren​(java.lang.String elementUri,
                                   java.lang.String elementLocalName)
        Selects the contents of the children elements with the given name.
        Parameters:
        elementUri - The URI of the elements to be selected.
        elementLocalName - The local name of the elements to be selected.
        Returns:
        Returns the contents of the selected elements.
      • selectAttribute

        XmlObject selectAttribute​(javax.xml.namespace.QName attributeName)
        Selects the content of the attribute with the given name.
        Parameters:
        attributeName - The name of the attribute to be selected.
        Returns:
        Returns the contents of the selected attribute.
      • selectAttribute

        XmlObject selectAttribute​(java.lang.String attributeUri,
                                  java.lang.String attributeLocalName)
        Selects the content of the attribute with the given name.
        Parameters:
        attributeUri - The URI of the attribute to be selected.
        attributeLocalName - The local name of the attribute to be selected.
        Returns:
        Returns the content of the selected attribute.