Learn Java & XML experience (1)

xiaoxiao2021-03-06  23

I have been free for a short period of time, and XML and the Dongdong associated with it have made some practical code and function. After doing it, I don't satisfy it, but the practice process is a typical example of a novice. So take it out to make the master pointing point. ^ _ ^ Since the B / S structure has been doing the Structure of the B / S, add a legacy issue (TREE type role and interaction between TREE type), so I want to be an XML solution. Of course, this problem has to be supported by modules for XML document objects. This experience is mainly for this issue. The design is as follows: XmlParser.class: XML parser interface. XmlParsrtest.class: XML parser interface implements subclasses. XmlParserFactory.class: XML parser factory class. XMLTool.Class: XML tool class interface (operation of XML document). XMLTooltest.class: XML tool class interface implements subclasses. XMLToolShow.class: {XML tool class interface implementation subclasses (all methods implementations are implemented with the corresponding method of the XMLTool interface, the function is to get better as XML processing tools with SETFORM () flexible. Efficiency and perfect.} XMLToolFactory.Class: XML tool class work factory. There are also Class: XMLBuilder.class in two design maps: Generate the specified XML string based on the specified document.Node. Xmloutput.class: According to the specified XML string or inputStream generates a specified XML file. I am using an XML4J parser in xmlparsrtest.class. The code is as follows:

Package com.ceic.workflow.xml.parser;

Import com.ceic.workflow.xml.xmlparser;

Import com.ibm.xml.parsers. *;

Import java.io. *;

Import org.xml.sax.inputsource;

/ **

* XML parser TEST

* Title: XML parser TEST

* Description: XML parser TEST

* Copyright: CopyRight (C) 2003

* Company: Guodian Information Center

* @Author Zhang Zhizhong

* @version 1.0

* /

Public Class XMLParsrtest Implements XMLParser

{

Public XMLParsrtest ()

{

}

/ **

* Analyze the XML file in the PATH position

* @PARAM PATH file location

* @Param Vali alternate parameters

* @Return Object

* @Roseuid 3ec2fc10043

* /

Public Object Parse (String Path, Boolean Vali)

{

IF (PATH! = null && path.length ()> 0) {

Try {

DOMPARSER PARSER = New DOMPARSER ();

Parser.Parse (PATH);

Return Parser.getDocument. )

} catch (exception e) {

System.out.println ("XmlParserTest.Parse (/" "

Path "/", " Vali ") error " E.GetMessage ());

Return NULL;

}

}

Return NULL;

}

/ **

* Analyze Xmlstring string * @Param Xmlstring XML string

* @Return Object

* @Roseuid 3ec2fc10043

* /

Public Object Parsestring (String Xmlstring)

{

IF (xmlstring! = null && xmlstring.Length ()> 0) {

Try {

DOMPARSER PARSER = New DOMPARSER ();

StringReader rd = new stringReader (Xmlstring);

InputSource IN = New InputSource (RD);

Parser.Parse (in);

Return Parser.getDocument. )

} catch (exception e) {

System.out.println ("XmlParsrtest.ParsString

(/ "" xmlstring "/") error " E.getMessage ());

Return NULL;

}

Return NULL;

}

}

XmlParserFactory.class is manually registered with ordinary manual. Master can give a good solution. code show as below:

Package com.ceic.workflow.xml;

Import com.ceic.workflow.xml.parser.xmlparsrtest;

Import java.util. *;

/ **

* XML parser factory

* /

Public Class XmlParserFactory

{

Private static hashtable table;

Private static string defaultType DEFAULTTYPE;

Private XmlParserFactory ()

{

}

/ **

* Get an XML parser

* @Param Parsertype XML parser type

* @Param ParserclassName XML parser class name

* @return com.ceic.Workflow.xml.xmlparser

* @ROSEUID 3ECB39E30029

* /

Public Static XmlParser getXmlParser

String Parsrtype, String ParserclassName)

{

Try {

IF (Parsertype == Null || Parsrtype.Length () <= 0) {

Parsertype = defaulttype;

}

IF (Table.Containskey (ParserType)) {

Return ((XMLParser) Class.Forname (Table.

Get (ParserType) .tostring ()). NewInstance ());

}

IF (ParserclassName! = null && paserclassname.length ()> 0) {

Try {

XmlParser Temp = (XMLParser) Class.

Forname (ParserclassName) .newinstance ());

IF (Parsrtype! = null && pasertype.length ()> 0) {

Table.Put (Parsertype, ParserclassName);

}

Return Temp;

} catch (exception EE) {

System.out.println (EE.GETMESSAGE ());

System.out.println ("Specified XML parser does not exist"); return null;

}

}

Return NULL;

} catch (exception e) {

System.out.println (E.getMessage ());

System.out.println ("The specified XML parser does not exist");

Return NULL;

}

}

Public static string getDefaultType () {

Return DefaultType;

}

Static

{

Table = new hashtable ();

Table.Put ("DOM", "com.ceic.workflow.xml.

Parser.xmlparsrtest ");

DefaultType = "DOM";

}

}

Where GetXmlParser (String ParserType, String ParserClassName) If you are calling known (registered) XMLParser, the second parameter can be null or "" interface XMLTool.class is the main external operation interface. code show as below:

Package com.ceic.workflow.xml;

Import org.w3c.dom. *;

/ **

* Title: XML processing tool operates and implements partial interface

* Description: The operation of the XML processing tool and the interface of the implementation

* Copyright: CopyRight (C) 2003

* Company: Guodian Information Center

* @Author Zhang Zhizhong

* @version 1.0

* XML processing tool operates and implements partial interface

* /

Public Interface XMLTool

{

/ **

* Generate a Java object by parsing the specified XML file through the parser.

* For example org.w3c.dom.document.

* @Param Path - XML ​​file path (including file name).

* @Param Vali - Alternate parameters. Parse in XMLPARSER

* (String Path, Boolean Vali) corresponds. The default is false.

* @Return Object

* @Roseuid 3ec1eab0007

* /

Public Object Build (String Path, Boolean Vali);

/ **

* Resolve the specified XML string to generate Java objects through the parser.

* For example org.w3c.dom.document.

* @Param Xmlstring XML string

* @Return Object

* @Roseuid 3ec1eab0007

* /

Public Object Build (String Xmlstring);

/ **

* Set the Document.

* @Param docs -document.

* /

Public void setdocument. Ource (Document.nbspdocs);

/ **

* Set the XML parser, each parser is different,

* It is recommended to solidify the analyzer in Class.

* @PARAM PARSERNAME - Parcer Name

* @Param ClassName -

* Resolver Class name, if it is a known parser (cured in XmlParserFactory),

* You can pass null or empty strings

* @Roseuid 3ec1eba0366

* /

Public void setParser (String Parsername)

String classname);

/ *** Set a single attribute of the specified node

* @Param Nodename Name Name

* @Param PropertyName Attribute name,

* If you specify the node value of the NodeName node for your space

* @Param Value Attribute or node value

* @Param setall is updated all nodes

* @Roseuid 3ecb3fa50317

* /

Public void setProperty (String Nodename, String

PropertyName, String Value, Boolean Setall;

/ **

* Get a single attribute of the specified node

* @Param Nodename Name Name

* @Param PropertyName Attribute name,

* If it is empty, find the value of the node.

* @Return String

* @Roseuid 3ecb3fa50317

* /

Public String getProperty (String Nodename,

String propertyName);

/ **

* Get specified properties of all nodes of the specified node name

* @Param Nodename Name Name

* @Param PropertyName Attribute name,

* If it is empty, find the replacement of the node.

* @Return String []

* @Roseuid 3ecb3fa50317

* /

Public String [] getProperty (String Nodename,

String propertyName);

/ **

* Get XMLTool's basis for XMLTools from XMLToolFactory.

* @Param XmltoolName - XMLTool Type name in the factory class.

* @Param ClassName - the class name of XMLTool.

* The default is NULL or empty string.

* @ROSEUID 3ec1d870379

* /

Public void setFormat (String XmlToolName,

String classname);

/ **

* At all PARENT nodes or specified Parent nodes (specific location by setmarksign (),

* SetMark () or setmarkadv () to determine) Add new node NodeName

* @Param Parent Node name to add a child node

* @Param nodename child node

* @Param addall Add all PARENT nodes

* @ROSEUID 3ECB40D10250

* /

Public void addnode (String Parent, Node NodeName,

Boolean addall;

/ **

* At all PARENT nodes or specified Parent nodes (specific location by setmarksign (),

* setmark () or setmarkadv () to determine) Delete name

* Nodename Attribute and subpost

* @Param Parent to delete the node name of the node of the child or Attribute

* @Param nodename To delete the child node name or Attribute name

* @Param deLall deletes all NodeName child nodes and Attribute

* /

Public void Delnode (String Parent, String

Nodename, Boolean DeLall;

/ **

* At all PARENT nodes or specified Parent nodes (specific location by setmarksign (),

* setmark () or setmarkadv () to determine)

* Replace the node of NodeOLD to Nodenew * @Param Parent to operate the node name

* @Param nodeold The node name of the node to replace

* @Param nodenew Replace the node

* @Param Editall does not replace all Parent's child node nodeold to Nodenew

* /

Public void EditNode (String Parent, String NodeOLD,

Node nodenew, boolean editall;

/ **

* Set the location of XML document data (according to the coordinate position of the data)

* @Param Index coordinates

* @Return Boolean

* @ROSEUID 3ECB427B0357

* /

Public Boolean SetMark (int index);

/ **

* Set the location of the XML document data (according to the value of the data)

* @Param Name Attribute name, if you are empty, check the value of the node

* @Param Value's value or node to query the value of Attribute or node

* @Return Boolean

* @Roseuid 3ecb42bb03a9

* /

Public Boolean SetMark (String Name, String Value);

/ **

* Set the node name based on the setmark () method.

* @Param Nodename Name Name

* @Roseuid 3ecb43820157

* /

Public void setmarksign (String nodename);

/ **

* Set the location of XML document data (according to the value of the data, complex query)

* @Param Name Attribute name list, if you are empty, check the value of the node

* @Param Value lists for attribute or node to query

* @Param Operation Operation Algorithm. (In XMLTooltest subclass

* The default is "AND", only "and" and "or" operations)

* @Return Boolean

* /

Public Boolean SetMarkadv (String [] Name, String [] Value,

String operation);

/ **

* Create an empty document. Icon

* @Return Document. * /

Public document. Nbspcreatedocument. );

/ **

* Create an empty Element object called Name

* @Param Name Element name

* @Return Element

* /

Public element creteElement (String name);

/ **

* Create an empty Comment object called Name

* @Param Name Comment name

* @Return Comment

* /

Public Comment CreateComment (String Name);

/ **

* Create an empty Text object called Name

* @Param Name Text Name

* @Return TEXT

* /

Public text cretetext (string name);

/ **

* Create an empty ATTR object called Name

* @Param Name Attr Name

* @Return Attr

* /

Public Attr CreateAttr (String Name);

/ **

* According to the document operation. Interior of the XMLTool class

Document. ) Generate an XML file. * @Param path XML file location

* /

Public void output (String path);

/ **

* According to the document operation. Interior Document of the XMLTool class. )

* Rewrite the XML file specified by the build () method.

* /

Public void output ();

/ **

* Set the encoding that generates an XML file

* @Param Encod encoding, such as "UTF8" "GBK"

* /

Public void setENCODING (String Encod);

/ **

* Generate an XML file according to the specified Node object.

* @Param path XML file location

* @Param docu specified Node object

* /

Public void Output (String Path, Node Docu);

/ **

* Generate an XML file according to the specified Node object.

* @Param path XML file location

* @Param docu specified Node object

* @Param NOTOP is going down the head node name. Go out

* or remove the top

* and

* /

Public void Output (String Path, Node Docu, Boolean NOTOP);

/ **

* Generate an XML file based on the specified Node node name.

* @Param path XML file location

* @Param nodename specified Node node name

* @Param NOTOP is going down the head node name. Go out

* or remove

* Last layer and

* /

Public void output (String path, String Nodename,

Boolean NOTOP);

/ **

* Generate an XML file based on the specified Node node name.

* @Param path XML file location

* @Param nodename specified Node node name

* @Param Indexs NodeName Node location

* @Param NOTOP is going down the head node name. Go out

* or remove the top

* and

* /

Public void output (String path, String Nodename,

Int indexs, Boolean NOTOP);

/ **

* When setting the XML file, you will automatically add a wrap and space. (If DOCMENT is

* Generated by the XML file, generally already have spaces and wraps of empty nodes)

* No need to add spaces and wraps)

* /

Public void isenetablemake ();

/ **

* Setting up the wrap and space automatically when setting the XML file. (If Docment is

* Generated by the XML file, generally already have spaces and wraps of empty nodes)

* Don't be used to add spaces and wraps, default to this situation.

* Mainly used to handle the switches when multi-XML documentation with XMLTOOL.

* /

Public void isnotenablemakeup ();

/ **

* Get the name name of the current tag

* @Return String

* /

Public string getcurrentnodename ();

/ **

* Get the position number of the node name that is currently tagged.

* @Return Int * /

Public int getIndex ();

/ **

* Get the Document in the current XMLTool.

* @Return Document. * /

Public document. NBspGetDocument. Ource ();

/ **

* Set the head of XML when writing XML files

* /

Public void setheader;

}

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

New Post(0)