Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Creating and Parsing XML Documents in JDeveloper : Page 3

Oracle's XDK 10G extends JAXP to make reading, writing, and querying XML easy.


advertisement
Creating an XML Document (continued)
To create any XML document, you first create an XML declaration by setting the XML version and the document encoding for output:

xmlDocument.setVersion("1.0"); xmlDocument.setEncoding("UTF-8");

Then you create the remainder of the document nodes in sequence, creating each element and adding each element, starting with the root node. In this case, for example, create the root element using the createElement(String) method. Cast the Element object returned by createElement() method to XMLElement:

XMLElement catalogElement = (XMLElement) (xmlDocument.createElement("catalog"));

The XMLElement class implements the Element, ElementEditVAL, and NSResolver interfaces used for standard XML element features, DOM 3 Validation, and XPath namespace node selection respectively. In addition to the validation methods in ElementEditVAL, XMLElement class exposes overloaded validateContent() methods to validate an element.

Add the new root element to the XMLDocument object:



xmlDocument.appendChild(catalogElement);

Create the namespace element with the createElementNS(String, String) method:

XMLElement journalElement = (XMLElement) (xmlDocument.createElementNS( "http://xdk.com/catalog/journal","journal:journal"));

Add the journal element to the root element:

catalogElement.appendChild(journalElement);

Add the namespace attribute journal:title with the createAttributeNS(String, String, String) method:

journalElement.setAttributeNS( "http://xdk.com/catalog/journal", "journal:title", "Oracle Magazine");

You create the journal:publisher and journal:author elements similarly. Add both elements to the journal:journal element.

Next, create an XMLText node to set the text of the title element using the createTextNode(String) method:

XMLText title = (XMLText) xmlDocument.createTextNode( "Creating Search Pages");

Add the XMLText node to the journal:title element.

titleElement.appendChild(title);

The process to add the other elements and text nodes in the example XML document in Listing 1 is similar, so I won't list it exhaustively here. The XMLDocument class provides additional methods to create XML document elements other than those discussed in this section, so I've listed some of them in Table 3.

Table 3. The table shows the various XMLDocument methods to create XML document content.
Method Name Description
createCDATASection(
java.lang.String data)
Creates a CData section.
createComment(
java.lang.String data)
Creates a comment.
createEntityReference(
java.lang.String name)
Creates an entity reference.
createProcessingInstruction(
java.lang.String target,
java.lang.String data)
Creates a processing instruction.

When you've finished adding all the nodes, you output the XML document with the XMLPrintDriver class. First, create an OutputStream and then create an XMLPrintDriver by passing it the OutputStream.

OutputStream output = new FileOutputStream( new File( "c:/output/catalog.xml")); XMLPrintDriver xmlPrintDriver = new XMLPrintDriver( new PrintWriter(output));

 
Figure 4. Running the Application. Right click on the CreateXMLDocument.java item in JDeveloper and select Run from the popup menu.
Write the XML document to the output stream using the printDocument(XMLDocument) method.

xmlPrintDriver.printDocument(xmlDocument);

XMLPrintDriver can print not only an XMLDocument node, but other nodes as well. Table 4 lists additional print methods in the XMLPrintDriver class that you may find useful.

You can see the complete listing for the CreateXMLDocument.java in Listing 2.

To run the completed CreateXMLDocument.java application in JDeveloper, right click on CreateXMLDocument.java in the Applications Navigator and select "Run" (see Figure 4). The application should generate the XML document.

Table 4. The table shows useful print Methods in the XMLPrintDriver class.
Print Method Description
printAttribute(XMLAttr) Prints an attribute node.
printAttributeNodes(XMLElement) Prints attributes in an element node.
printCDATASection(XMLCDATA) Prints a CData section node.
printChildNodes(XMLNode) Prints the child nodes of a node.
printComment(XMLComment) Prints a comment node.
printDoctype(DTD) Prints a DTD.
printDocument(XMLDocument) Prints a document.
printDocumentFragment(XMLDocumentFragment) Prints a document fragment.
printElement(XMLElement) Prints an element node.
printEntityReference(XMLEntityReference) Prints an entity reference node.
printProcessingInstruction(XMLPI) Prints a processing instruction node.
printTextNode(XMLText) Prints a text node.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap