[Original] JDOM and XML analysis, PART 2

xiaoxiao2021-03-06  70

[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: 1 alice 2 Bob ResultSetBuilder class to determine the column name with the Query of the ResultSetMetadata class, and they as the name of the elements. The default, the root element has a name called "result", and each line has a name called "entry". These names can be changed via the setRootName (String Name) method and the StrowName (String Name) method. You can also use the setNamespace (Namespaces) method to name the namespace. If you want a ResultSet column description as an XML property rather than an element, you can call the setAttribute (String Column, String AttributeName) method - the name of the later method changes the name. You can also rename it with the setaselement (String ColumnName, String Elemname). All of these methods accept the index of the name. The following code snippet creates a document with these methods: ResultSetBuilder Builder = New ResultSetBuilder (RS); Builder.Setasttribute ("ID"); Builder.Setaselement ("Name", "FNAME"); Document Doc = builder.build () ;

alice

Bob

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:

Open Close
Sunday 11AM 4PM Monday 9AM 5pm

After the beta8 version, JDom provides built-in support for XPath, complete with org.jdom.xpath.xpath. In order to use XPath, you must first use XPath.newInstance () to construct an XPath instance. Xpath XPath = XPath.newInstance ("/ some / XPath"); then call SelectNodes () to get a list based on a given context answer. For example, this context can be a document or an element in a document. List results = xpath.selectnodes (doc);

There are other methods to get a single node, a digital value, a string value, and the like. The default xpath implementation can be used with jaxen, you can refer to http://jaxen.org. Listing2 code Find the information describing the Servlet deployment from the web.xml file. Give a web.xml file, Listing 3 shows a web.xml file: code Listing 2: XPath Pulls Information IMPORT JAVA.IO. *; Import Java.util. *; Import Org.jdom. *;

Import org.jdom.input. *; import org.jdom.output. *; import org.jdom.xpath. *;

public class XPathReader {public static void main (String [] args) throws IOException, JDOMException {if (args.length = 1!) {System.err.println ( "Usage: samples.XPathReader [web.xml]"); return ;} String filename = args [0]; PrintStream out = System.out; SAXBuilder builder = new SAXBuilder (); Document doc = builder.build (new File (filename)); // Print servlet information XPath servletPath = XPath.newInstance ("// servlet"); list servlets = servletpath.selectNodes (DOC); Out.println ("this war HAS" servlets.size () "registered servlets:"); item i = servlets.ITerator (); While (I.hasNext ()) {Element Servlet = (Element) i.next (); out.print ("/ t" servlet.getchild ("servlet-name") .getTextTrim () "for" servlet .getchild ("servlet-class") .Gettexttrim ()); list initparams = servlet.getchildren ("init-param"); ​​out.println ("(it has" initparams.size () "init params");} // print security role information // notice how we're directly fetching text content xpath rolepath = xpath.newinstance ("// security-role / role-name / text ()" ); List rolenames = rolepath.selectNodes (DOC); if (Rolenames.Size () == 0) {Out.Println ("This War Contains No Roles");} else {Out.println ("this War Contains" ROLENAMES.SIZE () "ROLES:"); i = ROLENAMES.ITERATOR (); while (I.hasNext ()) {Out.println ("/ T"

((Text) i.next ()). GetTextTrim ());}}}} code listing 3: EXAMPLE Web.xml File (for use with xpath process in listing 2)

snoop snoopservlet file viewfile initial 1000 The initial value for the Counter manager DIRECTOR PRESIDENT

The program output is as follows: This War Has 2 Registered Servlets: Snoop for SnoopServlet (It Has 0 Init Params) File for ViewFile (It Has 1 Init Params) This War Contains 3 Roles: Manager Director President

转载请注明原文地址:https://www.9cbs.com/read-121016.html

New Post(0)
CopyRight © 2020 All Rights Reserved
Processed: 0.053, SQL: 9