JSP's message book --- XML ​​implementation

xiaoxiao2021-03-06  45

Source: http://blog.yesky.com/blog/tiantian911/Archive/2005/02/12/73848.html

I have been watching XML recently, I found that XML is really a good stuff. With it, the exchange data has never been convenient. Today, I made my first XML app, huh, a message book based on XML technology.

Abandon the database, and use XML directly to store information, and then convert it through XSLT to HTML directly, it feels very easy.

The development tool uses Eclipse as Java IDE, XML operations using JDOM, XML file editing uses Oxygen XML Editor (very easy to use XML editor, pure Java).

This is Guestbook.xml, all messages are stored here.

XML Version = "1.0" encoding = "GB2312"

?>

DOCTYPE GUESTBOOK SYSTEM "File: / f: /site/xmlguestbook/guestbook.dtd"

>

XML-Stylesheet Type = "text / xsl" href = "guestbook.xsl"

?>

<

Guestbook

>

<

Message

>

<

Name

>

Vince Carter

Name

>

<

IP

>

202.113.66.66

IP

>

<

Content

>

A good site, do you think so?

Content

>

Message

>

<

Message

>

<

Name

>

Antony Walker

Name

>

<

IP

>

202.113.22.123

IP

>

<

Content

>

Vc isn't right here, you shouth be traded.

Content

>

Message

>

Guestbook

>

This is a DTD file, which is very convenient to use Oxygen XML Editor.

Element Message (#pcdata | name | ip | content) *

>

Element Content (#pcdata)

>

Element Guestbook (#pcdata | message) *

>

ELEMENT IP (#pcdata)

>

ELEMENT NAME (#pcdata)

>

This is a XSLT file:

XML Version = "1.0"

?>

<

XSL: Stylesheet

XMLns: XSL

= "http://www.w3.org/tr/wd-xsl"

>

<

XSL: Template

Match

= "/"

>

<

HTML

>

<

Body

>

<

TABLE

Border

= "1"

Bgcolor

= "Yellow">

<

TR

>

<

TH

>

Name

TH

>

<

TH

>

IP

TH

>

<

TH

>

Content

TH

>

TR

>

<

XSL: For-Each

SELECT

= "Guestbook / Message"

>

<

TR

>

<

TD

> <

XSL: Value-of

SELECT

= "Name"

/>

TD

>

<

TD

> <

XSL: Value-of

SELECT

= "IP"

/>

TD

>

<

TD

> <

XSL: Value-of

SELECT

= "Content"

/>

TD

>

TR

>

XSL: For-Each

>

TABLE

>

Body

>

HTML

>

XSL: Template

>

XSL: Stylesheet

>

Tissue in form, clear and easy to understand. Here is the Java code.

/ *

* Create Date 2005-2-15

* /

Package com.tiantian.xmlguestbook;

//

com.tiantian

Import org.jdom.document; import org.jdom.jdomexception; import org.jdom.input.saxbuilder; import org.jdom.output.

*

Import java.io.

*

;

/ *

* * @Author Tiantian

* /

Public class xmlguestbookbean {public static

Void

Main (String [] args) {XmlGuestbookBean Exam

=

New

XMLGuestbookBean ();

//

Declare an example of XMLGuestBookbean

Exam.addMessage

"

James

"

,

"

202.11.223.22

"

,

"

Hahah

"

);

//

Add a message, test

PUBLIC Element root

=

NULL

;

//

Define root

Public xmlguestbookbean () {

//

Empty constructor

} Private

Void

AddMessage (String Name, String IP, String Content) {

Try

{SAXBUILDER SB

=

New

Saxbuilder (); Document Doc

=

Sb.build

"

Guestbook.xml

"

Root

=

Doc.getrootElement ();

//

Take the root element

Element newmessage

=

New

ELEMENT

"

Message

"

Element NewName

=

New

ELEMENT ("

Name

"

Element NewContent

=

New

ELEMENT

"

Content

"

Element newip

=

New

ELEMENT

"

IP

"

NewName.Settext (Name);

//

Fill in the name

NewContent.Settext (Content);

//

Fill in settings

Newip.setText (IP);

//

IP

NewMessage.AddContent (newname);

//

Add a name to Message

NewMessage.AddContent (newip);

//

Add IP address to Message

NEWMESSAGE.AddContent (NewContent);

//

Add message content to Message

Root.addContent (NewMessage);

//

Add Message to the root

Format Format

=

Format.getCompactFormat (); format.setencoding

"

GB2312

"

);

//

Set the character of the XML file for GB2312

Format.SetinDent

"

"

XMLOUTPUTTER XMLOUT

=

New

XMLOUTPUTTER (FORMAT);

//

After the element is wrapped, the elements of each layer are protected from four grids.

XMlout.output (DOC,

New

FileOutputStream

"

Guestbook.xml

"

));

//

Output to file

}

Catch

FILENOTFOUNDEXCEE) {

//

file not found

E.PrintStackTrace ();

Catch

(JDOMEXCEPTION E) {

//

Jdom is abnormal

E.PrintStackTrace ();

Catch

IOEXCEPTION E) {

//

IO unusual

E.PrintStackTrace ();}}}

After using the format of Eclispe, it is beautiful, huh, huh. Just use the call in JSP / servlet, you can use the XML file directly, is it more clear and clear than the last JSP implementation?

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

New Post(0)