[Original] JDOM and XML analysis, PART 2
Working with namespace JDOM provides rich and local support for XML namespace. After the namespace is released, JDom is released. In JDOM, the namespace is described by the Namespace class: namespace xhtml = namespace.getNameSpace ("XHTML", "http://www.w3.org/1999/xhtml";);
Through construction, an object is given a name and can be free to a named space: ELT.ADDCONTENT (New Element ("Table", XHTML);
If no namespace given, the constructed element will have no namespace. The namespace of an element is part of its nature, so Jdom ensures that the elements move to other locations in the document will not be changed. If an element does not have a named space and move it below an element with namespace, it does not inherits the namespace. Sometimes this will cause confusion until you learn to separate the Textual description from a semantic structure. The XMLOUTPUTTER class selects a named space and guarantees that all "XMLns" declarations in the appropriate location. The default, class declaration position is required. If you want them to be further declared as a tree, there is a guideline with the Element.AddNameSpaceDecalaration () method. All JDOM elements and attribute access methods support a selectable namespace parameters, which indicates which namespaces are viewed. The following example points to the namespace "XHTML": list kids = html.getchildren ("Title", XHTML); Element Kid = HTML.GetIld ("Title", XHTML); Attribute Attr = Kid.GetaTRibute ("ID", XHTML);
When the adjustment of the access method, only the Unified Resource Locator (URLS) is related. This is because XML Namespaces works. If you do not provide a namespace instance for your access method, you will search for elements without namespace. JDOM is described in a very surface description method. No namespace means no namespace, no higher namespaces or new bugs may be generated. About ResultSetBuilderResultsetBuilder is an extension to JDOM, for those who need to use XML documents to process SQL result sets. You can find it in org.jdom.conrib.input. The ResultSetBuilder constructor accepts a java.sql.Resultset as an input parameter and returns an org.jdom.Document from its build () method. Statement stmt = connection.createStatement (); ResultSet rs = stmt.executeQuery ( "select id, name from registry"); ResultSetBuilder builder = new ResultSetBuilder (rs); Document doc = builder.build ();
If you do not provide the specified configuration information, the above code constructed document is similar to:
entry>
The ResultSetBuilder class does not provide any mechanism to store XML documents in the database. To achieve this task, you can use a local XML library, such as Oracle9i to set the performance of XML DB. Built-in XSLT now we ignore the basic thing of the core library, see some high performance processing, such as XSLT. XSLT provides standard methods for converting XML documents from a format to another format. Use an XML file to deal with changes, usually use the existing XML as an XHTML web page, or transform the XML document from a mode to another. JDOM built-in supports XSLT transformation in memory, and implements the XSLT engine with the JAXP standard interface. The key class is JdomSource and JDomResult, both in the org.jdo.transform package. JDOMSource provides a JDOM document as a conversion input, and JDomResult captures a result as a JDOM document. Listing 1 demonstrates a full program based on memory conversion: Code Listing 1: XSL Transform Import org.jdom. *; Import Org.jDom.Input. *; Import Org.jdom.Output. *;
Import org.jdom.transform. *; import javax.xml.transform. *; import javax.xml.transform.stream. *; public class xsltransform {
public static void main (String [] args) throws Exception {// Build the base document, just assume we get 2 params String docname = args [0]; String sheetname = args [1]; SAXBuilder builder = new SAXBuilder (); Document doc = builder.build (docname);
// use jaxp to get a transformer transformer transformer = transformerfactory.newinstance () .NewTransformer (New StreamSource (SheetName);
// Run the transformation jdomsource source = new jdomsource (doc); jdomResult result = new jdomResult (); transformer.transform (Source, Result);
Document doc2 = result.getdocument ();
// Display the results xmloutputter outp = new xmloutputter (); outp.setTextNormalize (TRUE); OUTP.SETINDENT (""); Outp.SetNewlines (true); Outp.output (DOC2, System.out);}
}
You can mix and match the implementation of a variety of sources and results. For example, if you know that you just intend to output a document and don't need a memory-converted JDOM description, you can replace it with an Import Javax.xml.Transform.stram.StreamResult: jdomsource source = new jdomsource (DOC); streamResult Result = New streamResult (System.out); Transformer.Transform (Source, Result);
XPath IncludedXPath provides a mechanism for reviewing an XML document using a string search path. With XPath, you can avoid traversal documents, simply parsing the information you want by simple path expression. For example, let's look at the XHTML document below: