Jdom (ZT)

xiaoxiao2021-03-06  42

JDOM

(Shi Di, Idealist@gcn.net.tw, 2002/4/1)

ABSTRACT

In Java's world, to manipulate XML, the easiest way is to use JDOM. Here I simply introduce how to use JDom to create an XML from an unsuspeted, how to use the XML File read process and how to use JDO to transfer XSLT to HTML.

JDOM is written by Brett McLauhlin and Jason Hunter, available for free in http://www.jdom.org.

2. Establish, new, delete, output

Example 1 shows how to establish, new, delete, and output a XML, each XML is represented by a document category in JDOM, so we have to first new document, you should refer to the root node when constructing Document, if not Indicate, the construction should be set by the setrootElement. Basically XML can be chemically formed into a tree structure, so after generating a root node, add a child node by the root node, and the new way is to call Element.addContent to join the new node. Delete If you know the child's node below a node, you can call Element.getChildren to get all the child nodes, and find out the point to delete the point to delete.

After the XML is established, we will usually want to output it to the file. JDom provides the XMLOUTPUTTER category for us to output, and set the encoding method before output. Otherwise, it will be preset with UTF-8, setnewlines is Indicates whether you want to help us on the output, easy to read, if you don't add this line of instructions, the preset will not break, then all information will be on the same line.

===== Example 1 =====

Import Org.jdom. *;

Import Org.jdom.Output. *;

Import java.io. *;

Import java.util.list;

Public class mycreate {

Public mycreate () {

String [] strphone = {"XML Brief", "Writing XML", "Analyzing XML", "Wanted XML", "Upgnent XML", "Convert XML"};

ELMTROOT = New Element ("article");

Document Docjdom = New Document (Elmtroot);

// new

For (int i = 0; i <6; i ) {

ELMTCHAPTER = New Element ("chapter");

Elmtchapter.addContent (Strchapter [i]);

Elmtchapter.setttribute ("sort", new integer (i) .tostring ());

Elmtroot.addContent (Elmtchapter);

}

//delete

List Lstchapter = ElmTroot.getChildren ("chapter");

Lstchapter.Remove (4);

// output

OutputXML (Docjdom, "E: /MYJDOM.XML");

}

Private void outputXML (Document Docxml, String Strfilename) {XMloutPutter FMT = New XMLOUTPUTTER ();

Try {

FMT.SETENCODING ("BIG5");

FMT.setnewlines (TRUE);

FileWriter FWXML = New FileWriter (STRFILENAME);

FMT.Output (Docxml, FWXML);

FWXML.CLOSE ();

}

Catch (IOException E) {

E.PrintStackTrace ();

}

}

Public static void main (String [] args) {

Mycreate mycreate1 = new mycreate ();

}

}

3. Read into a XML File

One of the most common fields that XML most often applied is probably as a setting file, so how to read a existing XML from the file.

JDM uses SAX or DOM to analyze XML, with SAX will be more than DOM, so it is usually used with SAX, if you are not familiar with SAX, at http://www.saxproject.org/ related file information.

Example 2 is to establish Document with SAX. When establishing SAXBUILDER, the parameters are incorporated by the DTD testing XML, True means that false said not. When the call is build, Jdom has helped us establish a XML Tree with SAX, so we can easily find the node we want in Tree.

===== Example 2 =====

Import Org.jdom. *;

Import org.jdom.Input. *;

Import Org.jdom.Output. *;

Import java.io. *;

Import java.util.list;

Public class myread {

Public myread () {

Document docjdom;

// Establish Document with SAX

SAXBUILDER BSAX = New Saxbuilder (FALSE);

Try {

DOCJDOM = BSAX.BUILD (New File ("E: /MYJDOM.XML"));

}

Catch (jdomexception e) {

E.PrintStackTrace ();

Return;

}

// Add a new child node in the root node

ELMTROOT = DOCJDOM.GETROOTELEMENT ();

ELMTCHAPTER = New Element ("chapter");

Elmtchapter.Settext ("Upgnent XML");

Elmtchapter.setttribute ("sort", "4");

Elmtroot.addContent (Elmtchapter);

// Print the child node of all root nodes

List lstchildren = Elmtroot.getChildren ("chapter");

For (int i = 0; i

Element Elmtchild = (Element) Lstchildren.get (i);

System.out.println ("Child" i);

System.out.println ("Text:" Elmtchild.getText ()); System.out.Println ("Attribute:" Elmtchild.GetaTRibutevalue ("sort");

}

// Output to the file

OutputXML (Docjdom, "E: /MYJDOM2.XML");

}

Private void OutputXML (Document Docxml, String Strfilename) {

XMloutPutter FMT = New XMLOUTPUTTER ();

Try {

FMT.SETENCODING ("BIG5");

FMT.setnewlines (TRUE);

FileWriter FWXML = New FileWriter (STRFILENAME);

FMT.Output (Docxml, FWXML);

FWXML.CLOSE ();

}

Catch (IOException E) {

E.PrintStackTrace ();

}

}

Public static void main (String [] args) {

MYREAD MyRead1 = new myread ();

}

}

4. Convert XML to HTML

To convert XML to html, you must write XSL first, but even if you write XSL, Jdom does not provide XML conversion. Fortunately, there will be Apache's Xalan after JDOM, it can help us do this.

To use apache's Xalan, first determine Xalan.jar in the classpath. The first usage of Xalan is to execute in the command, the instruction is as follows ...

C: /> java org.apache.xalan.xslt.process -in xml file-xsl xsl sample -Outout output file name

The usage of the command column is very convenient in general use, but we are interested in how to use in the program. Example 4 provides a simple SAMPLE.

===== Example 4 =====

Import javax.xml.transform.transformerfactory;

Import javax.xml.transform.transformer;

Import javax.xml.transform.transformerexception;

Import javax.xml.transform.transformerConfigurationException;

Import javax.xml.transform.stream. *;

Public class mygenhtml {

Public Mygenhtml (String Strxml, String Strxsl) {

Try {

Genhtml (Strxml, Strxsl, strHtml);

}

Catch (TransformerConfigurationException E) {

E.PrintStackTrace ();

}

Catch (Transformerexce E) {

E.PrintStackTrace ();

}

}

Private Void Genhtml (String Strxmlfile, String String String Strings TransformerConfigurationException, TransformerException

{

TransformerFactory myfactory = transformerfactory.newinstance (); transformer myTransformer = MyFactory.NewTransformer (STRXSLFILE);

MyTransformer.Transform (New StreamSource (Strxmlfile), New StreamResult (strHtmlfile);

}

Public static void main (String [] args) {

Mygenhtml mygenhtml1 = new mygenhtml (args [0], args [1], args [2]);

}

}

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

New Post(0)