Learn Java & XML experience (2)

xiaoxiao2021-03-06  23

XMLTooltest.class implements the operation of the XML document. Includes a new XML Document. Structure and content or document from the specified XML file or document. Structure, Output to the specified XML file (or update the XML file), get and set the specified property (or list) of the specified node name, add a sub-node, delete the sub-node, edit the sub-node (first delete first) In the addition, to be improved), support in the document (subscript positioning, and query positioning, such as: Name attribute) of the Test Node, "Zhang Zhizhong" node, there is a simple composite query positioning, temporarily all And Relationship. :)), can be used in combination with a comprehensive operation (a bit of a database of a bit), and the application you want to do). Later, I made a database and XML pairing example. code show as below:

Package com.ceic.workflow.xml.tool;

Import 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

* XMLTool implementation subclass, where the XML parser is obtained from XMLPARSerFactory.

* Nothing to explain the XMLTOOL interface.

* /

Public Class XMLTooltest Implements XMLTOOL

{

Private XmlParser Parser;

Private string parsrtype =

XMLPARSERFAACTORY.GETDEFAULTTYPE ();

Private string parserclassname = "";

Private int index = 0;

Private string markname = ""

PRIVATE DOCUMENT. Nbspdoc;

Private int defaultbegin = 0;

Private string encoding = "utf8";

Private string filepath = ""

PRIVATE BOOLEAN EnableMakeUp = false;

Private string header = "";

Public XMLTooltest ()

{

}

Public Object Build (String Path, Boolean Vali)

{

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

Parser = XmlParserFactory.GetXmlParser

(Parsertype, ParserclassName);

IF (Parser! = NULL) {

DOC = (Document.Parser.Parse (Path, Vali);

Filepath = path;

Return DOC;

}

}

Return NULL;

}

Public Object Build (String Xmlstring)

{

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

Parser = XmlParserFactory.GetXmlParser

(Parsertype, ParserclassName);

IF (Parser! = NULL) {

DOC = (Document.Parser.ParsString (Xmlstring);

FilePath = "";

Return DOC;

}

}

Return NULL;

}

Public void setdocument. OURCE (Document.nbspdocs) {

DOC = DOCS;

}

Public void setParser (String Parasname)

{

IF (Parsername! = Null && Parsername.length ()> 0) {

Parsertype = parse;

ParserclassName = ClassName;

}

}

Public void setProperty (String Nodename, String

PropertyName, String Value, Boolean Setall) {

Try {

Nodelist list = doc.getElementsBytagname (nodename);

IF (list.getLength ()> 0 && value! = null && value.length ()> 0)

{

IF (setall) {

For (int i = 0; i

IF (PropertyName! = null&propertyname.length ()> 0)

{

(ELEMENT) List.Item (i)). SetAttribute

(PropertyName, Value);

}

Else {

List.item (i) .SetNodeValue (Value);

}

}

} else {

IF (PropertyName! = Null && propertyName.length ()> 0) {

IF (NodeName.equals (Markname)) {

(ELEMENT) List.Item (Index)). SetAttribute

(PropertyName, Value);

}

Else {

(ELEMENT) List.item (Defaultbegin)).

SetAttribute (propertyname, value);

}

} else {

IF (NodeName.equals (Markname)) {

List.item (index) .SetNodeValue (Value);

}

Else {

List.item (Defaultbegin) .SetnodeValue (Value);

}

}

}

}

} catch (exception EE) {

System.out.println ("Set" NodeName "node"

PropertyName "Error");

}

}

Public String getProperty (String Nodename,

String propertyName)

{

Try {

Nodelist list = doc.getElementsBytagname (nodename);

IF (List.getLength ()> 0) {

IF (PropertyName! = Null && propertyName.length ()> 0) {if (nodename.equals (markname)) {

Return ((Element) List.Item (Index)). GetAttribute

(PropertyName);

} else {

Return ((Element) List.Item (DefaultBegin)).

GetAttribute (propertyName);

}

} else {

IF (NodeName.equals (Markname)) {

Return ((Element) List.Item (Index)). GetNodeValue ()

} else {

Return ((Element) List.Item (DefaultBegin)).

GetNodeValue ()

}

}

}

Return NULL;

} catch (exception EE) {

System.out.println ("GetProperty (" NodeName ","

PropertyName "); error" EE.GETMESSAGE ());

Return NULL;

}

}

Public String [] getProperty (String Nodename,

String propertyName)

{

Try {

String [] TEMP;

Nodelist list = doc.getElementsBytagname (nodename);

IF (List.getLength ()> 0) {

IF (PropertyName! = Null && propertyName.length ()> 0) {

Temp = new string [list.getLength ()];

For (int i = 0; i

Temp [I] = ((Element) List.Item (i)).

GetAttribute (propertyName);

}

Return Temp;

} else {

Temp = new string [list.getLength ()];

For (int i = 0; i

Temp [I] = ((Element) List.Item (i)). getnodevalue ()

}

Return Temp;

}

}

Return NULL;

} catch (exception EE) {

System.out.Println ("GetProperty", " "

PropertyName "); error");

Return NULL;

}

}

Public string getparent (string returnattr) {

String Tempreturn = ""

String TempNode = MarkName

int Tempindex = Index

Nodelist List = Doc.GetElementsBytagname (Markname);

Node node = list.item (index);

IF (Returnattr! = Null && Returnattr.Length ()> 0) {

Tempreturn = ((Element) node.getParentNode ()).

GetaTtribute (Returnattr);

} else {

Tempreturn = node.getParentNode (). getnodevalue ()}

Return Tempreturn;

}

Public void setFormat (String XmlToolName,

String classname) {}

Public void addnode (String Parent, Node NodeName,

Boolean Addall

{

Try {

Nodelist list = doc.getlementsBytagname (PARENT);

IF (List.getLength ()> 0) {

IF (addall) {

For (int i = 0; i

INT type = nodename.getnodetype ()

Switch (Type) {

Case node.attribute_node: {

(ELEMENT) List.Item (i)). SetAttribute (((attr)

NodeName) .getname (), ((attr) nodename) .getValue ());

}

DEFAULT: {

List.Item (i) .appendchild (nodename);

}

}

}

} else {

IF (parent.equals (markname)) {

INT type = nodename.getnodetype ()

Switch (Type) {

Case node.attribute_node: {

(ELEMENT) List.Item (Index)). SetAttribute

((Attr) nodename) .getname (), ((attr) nodename) .getValue ());

}

DEFAULT: {

List.Item (Index) .appendchild (Nodename);

}

}

} else {

INT type = nodename.getnodetype ()

Switch (Type) {

Case node.attribute_node: {

(ELEMENT) List.item (Defaultbegin)).

SetAttribute ((attr) nodename) .GetName (),

((Attr) nodename) .GetValue ());

}

DEFAULT: {

List.item (Defaultbegin) .appendchild (nodename);

}

}

}

}

}

} catch (exception e) {

System.out.println ("Add Node error");

}

}

Public void Delnode (String Parent, String Nodename,

Boolean deLall)

{

Try {

Nodelist list = doc.getlementsBytagname (PARENT);

IF (List.getLength ()> 0) {

IF (deLall) {

For (int i = 0; i

Try {

(ELEMENT) List.Item (i)). RemoveAttribute (Nodename);

} catch (exception EE) {}

Nodelist TList = List.Item (i) .GetchildNodes ()

IF (TList.GetLength ()> 0) {

For (int K = 0; k

IF (NodeName.Equals (TLIST.ITEM (K) .Getnodename ())) {

Short type = tlist.item (k) .GetnodeType () ife (type == node.attribute_node) {

(ELEMENT) List.Item (i)). RemoveAttribute

(Nodename);

}

Else List.Item (i) .removechild (TList.Item (k));

Break;

}

}

}

}

} else {

IF (parent.equals (markname)) {

Try {

(ELEMENT) List.item (Index)). RemoveAttribute (Nodename);

} catch (exception EE) {}

Nodelist tlist = list.item (index) .GetchildNodes ()

IF (TList.GetLength ()> 0) {

For (int K = 0; k

IF (NodeName.equals (TLIST.ITEM (k).

GetNodename ())) {

Short type = tlist.item (index) .GetNodetyPE ()

IF (Type == Node.attribute_node) {

(ELEMENT) List.Item (Index)).

REMOVEATTRIBUTE (NODENAME);

}

Else List.Item (Index) .removechild

(TLIST.ITEM (k));

Break;

}

}

}

} else {

Try {

(ELEMENT) List.Item (DefaultBegin)). Removeattribute

(Nodename);

} catch (exception EE) {}

Nodelist TList = List.item (DefaultBegin).

getChildNodes ()

IF (TList.GetLength ()> 0) {

For (int K = 0; k

IF (NodeName.equals (TLIST.ITEM (k).

GetNodename ())) {

Short type = tList.Item (index).

GetNodetyPE ()

IF (Type == Node.attribute_node) {

(ELEMENT) List.item (Defaultbegin)).

REMOVEATTRIBUTE (NODENAME);

}

Else List.item (Defaultbegin).

RemoveChild (TList.Item (k));

Break;

}

}

}

}

}

}

} catch (exception e) {

System.out.println ("Delete Node error");

}

}

Public void EditNode (String Parent, String NodeOLD,

Node Nodenew, Boolean Editall

{

Try {

Nodelist list = doc.getlementsBytagname (PARENT);

IF (List.getLength ()> 0) {

IF (editall) {

For (int i = 0; i

Nodelist TList = List.Item (i) .GetchildNodes ()

IF (TList.GetLength ()> 0) {

For (int K = 0; k

IF (NodeOLD.EQUALS (TLIST.ITEM (K) .Getnodename ())) {list.item (i) .replacechild (Tlist.Item (k), nodenew);

Break;

}

}

}

}

} else {

IF (parent.equals (markname)) {

Nodelist tlist = list.item (index) .GetchildNodes ()

IF (TList.GetLength ()> 0) {

For (int K = 0; k

IF (NodeOLD.EQUALS (TLIST.ITEM (k) .Getnodename ())) {

List.item (Index). Repelacechild (TList.Item (K),

Nodenew;

Break;

}

}

}

} else {

Nodelist TList = List.item (DefaultBegin).

getChildNodes ()

IF (TList.GetLength ()> 0) {

For (int K = 0; k

IF (NodeOLD.EQUALS (TLIST.ITEM (k) .Getnodename ())) {

List.item (Defaultbegin) .replacechild

(TLIST.ITEM (k), Nodenew);

Break;

}

}

}

}

}

}

} catch (exception e) {

System.out.println ("Replace Node error");

}

}

Public Boolean SetMark (int index)

{

Nodelist List = Doc.GetElementsBytagname (Markname);

IF (list.getLength ()> 0 && index <= list.getlength () -

1 && index> = 0) {

Index = index;

Return True;

}

Return False;

}

Public Boolean SetMark (String Name, String Value)

{

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

Try {

INT i = 0;

Nodelist List = Doc.GetElementsBytagname (Markname);

Boolean attrs = false;

Boolean Find = False;

IF (Name! = null && name.Length ()> 0) attrs = true;

IF (List.getLength ()> 0) {

For (i = 0; i

IF (attrs) {

IF (value.equals ((Element) List.Item (i)).

GetaTtribute (name)))) {

Find = true;

Break;

}

}

Else {

IF (value.equals (list.item (i) .GETNODEVALUE ())) {

Find = true;

Break;

}

}

}

IF (find) {

INDEX = I;

Return True;

} else {

Return False;

}

}

} catch (exception e) {

System.out.println ("STRING NAME,

String value) Invalid "); Return False;

}

}

Return False;

}

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

String Operation) {

INT Tempindex = Index;

Try {

IF (name! = null && name.length> 0 && value! = null&igue.

Length> 0 && name.length == value.length) {

IF (Operation == Null || Operation.Length () == 0)

Operation = "AND";

IF (! Operation.equals ("AND") &&! Operation.equals ("OR"))

Operation = "AND";

IF (Operation.Equals ("and")) {

Boolean Find = True;

int [] results = new int [name.length];

For (int i = 0; i

IF (SetMark (Name [i], value [i])) {

Results [i] = index

IF (i> 0) {

IF (Results [i]! = Results [i-1]) {

Find = false;

Break;

}

}

} else {

Find = false;

Break;

}

}

IF (find) {

Return True;

} else {

INDEX = Tempindex;

Return False;

}

} else {

Boolean thefind = false;

For (int J = 0; j

IF (SetMark (Name [J], Value [J])) {

Thefind = True;

Break;

}

}

IF (thefind) {

Return True;

} else {

Return False;

}

}

}

Return False;

} catch (exception ex) {

System.out.println ("STMARKADV (String [] Name,

String [] Value, String Operation) Error: " ex.getMessage ());

INDEX = Tempindex;

Return False;

}

}

Public void setMarksign (String Nodename)

{

Markname = nodename;

}

Public document. Nbspcreatedocument. ) {

Try {

Document. nbspnode = ((Document.class.Forname

("com.ibm.xml.dom.document.mpl"). NewInstance ())

Return node;

} catch (exception ex) {

System.out.print ("Come can't find com.ibm.xml.dom.document.mpl");

Return NULL;

}

}

Public element creteElement (String name) {

Return Doc.createElement (Name);

Public Comment CreateComment (String Name) {

Return Doc.createComment (Name);

}

Public text cretetext (string name) {

Return Doc.createTextNode (Name);

}

Public Attr CreateAttr (String Name) {

Return Doc.createAttribute (Name)

}

Public void output (string path) {

Output (Path, Encoding, Doc, False);

}

Public void output () {

Output (Filepath, Encoding, Doc, False);

}

Public void setEncoding (String Encod) {

Encoding = encod;

}

Public void iSenablemakeUp () {

Enablemakeup = true;

}

Public void isnotenablemakeup () {

Enablemakeup = false;

}

Public void output (string path, node docu) {

Output (Path, Encoding, Docu, False);

}

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

Output (Path, Encoding, Docu, NOTOP);

}

Public void output (String path, String Nodename,

Boolean NOTOP) {

Nodelist list = doc.getElementsBytagname (nodename);

Node TempNode;

IF (List.getLength ()> 0) {

IF (NodeName.equals (Markname)) {

TempNode = List.Item (getIndex ());

} else {

TempNode = List.item (Defaultbegin);

}

Output (Path, Encoding, TempNode, NOTOP);

}

}

Public void output (String path, String Nodename,

Int indexs, boolean NOTOP) {

Nodelist list = doc.getElementsBytagname (nodename);

Node TempNode;

IF (List.getLength ()> 0) {

IF (indexs <= list.getLength () -1) {

TempNode = List.Item (Indexs);

}

Else {

IF (NodeName.equals (Markname)) {

TempNode = List.Item (getIndex ());

} else {

TempNode = List.item (Defaultbegin);

}

}

Output (Path, Encoding, TempNode, NOTOP);

}

}

Private void output (String path, String Encod,

Node Docu, Boolean NOTOP) {

Try {

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

Filepath = path;

}

IF (Encod! = null && encod.length ()> 0) {

Encoding = encod;

}

XMLBuilder Builder = new xmlbuilder (); if (! Enablemakeup) {

Builder.SetinDent ("");

Builder.SetLineSeparator ("");

}

Builder.setXMLHeader (header);

Builder.PrintDomtree (Docu, NOTOP);

String result = builder.getxmlResult ()

XMloutput out = new xmloutput (filepath);

Out.setencoding (encoding);

Out.output (result);

} catch (exception EE) {

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

System.out.println ("Write XML file:" FilePath "Error");

}

}

Public int getIndex () {

Return Index;

}

Public string getcurrentnodename () {

Return Markname

}

Public document. NBspGetDocument. Ource () {

Return DOC

}

Public void sethead (string header) {

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

Header = header;

}

}

}

XmltoolFactory.class code is as follows:

Package com.ceic.workflow.xml;

Import com.ceic.workflow.xml.tool.xmltooltest;

Import java.util. *;

/ **

* XMLTool's factory class. Provide an operation and implementation of an XML processing tool for other classes.

* /

Public Class XMLToolFactory

{

Private static hashtable table;

Private static string defaultType DEFAULTTYPE;

PRIVATE STATIC STRING OWER;

/ **

* @ROSEUID 3ECC2FC101BF

* /

Private XmltoolFactory ()

{

}

/ **

* Get a common XMLTool.

* @Return com.ceic.Workflow.xml.xmltool

* @ROSEUID 3ECC2243019B

* /

Public static xmltool getXmltool ()

{

Try {

Return ((XMLTool) Class.Forname (Table.Get (Ower).

TOSTRING ()). Newinstance ());

} catch (exception EE) {

System.out.Println ("Get Common XMLTool error");

Return NULL;

}

}

/ **

* Get an XMLTOOL. A subclass of XMLTool is obtained by parameters.

* @Param Type - XMLTool has implemented the Type name in XmlToolFactory

* @Param ClassName -

* XMLTool implements subclasses of Class name (including package name),

* Such as com.ceic.workflow.xml.tool.xmltooltest

*. The default is a null or empty string.

* @Return com.ceic.Workflow.xml.xmltool

* @ROSEUID 3ECC2243019B * /

Public Static XMLTool getXmltool (String Type,

String classname)

{

Try {

IF (type == null || Type.Length () <= 0) {

TYPE = defaulttype;

}

IF (Table.ContainsKey (Type)) {

Return (XMLTool) Class.Forname (Table.Get (Type).

TOSTRING ()). Newinstance ());

}

IF (ClassName! = null && classname.length ()> 0) {

Try {

XMLTool Temp = (XMLTool) Class.Forname (classname).

Newinstance ());

IF (Type! = null && type.length ()> 0) {

Table.Put (Type, ClassName);

}

Return Temp;

} catch (exception EE) {

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

System.out.println ("Specify XMLTool does not exist");

Return NULL;

}

}

Return NULL;

} catch (exception e) {

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

System.out.println ("Specify XMLTool does not exist");

Return NULL;

}

}

Public static string getDefaultType () {

Return DefaultType;

}

Static

{

Table = new hashtable ();

Table.Put ("Test", "com.ceic.workflow.xml.tool.

XMLTooltest ");

Table.Put ("Show", "com.ceic.workflow.xml.tool.

XmltoolShow ");

DefaultType = "Test";

Ower = "show";

}

}

Up to now, XMLParser and XMLTool are introduced. Next introduction to XMLBuilder.class, XMloutput.class, and some applications for them.

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

New Post(0)