Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

Pro XML Development with Java Technology 2006 phần 7 ppsx
Nội dung xem thử
Mô tả chi tiết
210 CHAPTER 7 ■ BINDING WITH XMLBEANS
try { //Create a cursor
CatalogDocument catalogDocument = CatalogDocument.Factory
.parse(xmlFile);
XmlCursor cursor = catalogDocument.newCursor();
//Move cursor to start of root Element
cursor.toFirstContentToken();
//Move cursor to first child element
cursor.toFirstChild();
//Add an Element
cursor.beginElement("journal");
//Add an attribute
cursor.insertAttributeWithValue("publisher", "IBM developerWorks");
cursor.dispose();
//Output modified document
System.out.println(catalogDocument.toString());
} catch (IOException e) {
} catch (XmlException e) {
}
}
//Method to navigate an XML document
public void navigateXMLDocument(File xmlFile) {
try { //Create a CatalogDocument object and create a cursor
CatalogDocument catalogDocument = CatalogDocument.Factory
.parse(xmlFile);
XmlCursor cursor = catalogDocument.newCursor();
//Move cursor to start of root Element
cursor.toFirstContentToken();
//Move cursor to start of first child Element
cursor.toFirstChild();
//Move cursor to start of article element
cursor.toFirstChild();
//Move cursor to start of title element
cursor.toFirstChild();
System.out.println(cursor.getTextValue());
//Dispose cursor
cursor.dispose();
} catch (IOException e) {
} catch (XmlException e) {
}
}
Vohra_706-0C07.fm Page 210 Thursday, July 13, 2006 1:11 PM
CHAPTER 7 ■ BINDING WITH XMLBEANS 211
public static void main(String[] args) {
XMLBeansCursor xmlBeansCursor = new XMLBeansCursor();
File xmlFile = new File("catalog.xml");
xmlBeansCursor.navigateXMLDocument(xmlFile);
xmlBeansCursor.addElement(xmlFile);
xmlBeansCursor.selectWithXPath(xmlFile);
xmlBeansCursor.selectWithXQuery(xmlFile);
}
}
Summary
XMLBeans is an XML-to-Java binding and runtime framework that is similar to JAXB. You can use
the binding framework to bind an XML Schema to Java types. XMLBeans offers complete support for
all XML Schema constructs. You can use a binding configuration file to customize XML Schema to
Java type bindings. You can use the runtime framework to unmarshal and marshal an XML document to and from Java objects that are instances of bound Java types.
In addition to marshaling and unmarshaling an XML document, XMLBeans offers low-level
navigational support through the XmlCursor API. Using the XmlCursor API, you can position a
cursor at a specified location and modify the document content at that location. This API also provides
support for addressing document content with XPath and querying an XML document using the
XQuery language.
In our opinion, JAXB 2.0 should be the default choice for an XML Schema to Java types binding
framework, mainly because of the following reasons:
• It is part of the Java standards.
• It offers support for the bidirectional mapping between XML Schema content and Java types.
However, the following pragmatic reasons may indicate XMLBeans to be the more appropriate
choice:
• You are looking for full XML Schema support, but you are not ready to move to J2SE 5.0.
• During the marshaling process, you want low-level control over the XML markup contained
in the marshaled XML document.
• During the unmarshaling process, you want to use XPath to address specific nodes within the
XML document, or you want to use the XQuery language to query the content of an XML
document.
Vohra_706-0C07.fm Page 211 Thursday, July 13, 2006 1:11 PM
Vohra_706-0C07.fm Page 212 Thursday, July 13, 2006 1:11 PM