Mutual conversion between Java objects and XML files (including source code)

xiaoxiao2021-03-06  71

Requirements: JDK1.4 or more

Download: http://sparer.zj.com Example:

// root element must correspond to a Java class, here is WebApplicationLabject Doc = New DocumentObject ("Web-App", WebApplicationLabel.Class // Parameter "Web-App" corresponding root element label Name, the following // must correspond to a Java class with the element sub-node, here is servletLabel.java // ElementObject serv = doc.addchild (ElementObject.Multi, "servlet", servletlabel.class, " "); // You can have 0 to N servlet (corresponding parameter ElementObject.multi) in this element, in WebApplicationLabel.java // must have a property servletlabel [] servlets (here you can The last parameter corresponding to the next row) and its set and get method // If there are 1 to N servlet elements in the web-app element, the first parameter is ElementObject.morethanone, and in WebApplicationLabel.java / / Must have a property servletlabel [] servlets and its set and get method // If only one servlet element is allowed in the web-app element, the first parameter is ElementObject.single, and in WebApplicationLabel.java // must There is a property servletLabel servlet and its set and get method // If there is 0 or 1 servlet element in the web-app element, the first parameter is ElementObject.Option, and there must be one in WebApplicationLabel.java // must have Property ServletLabel Servlet and Its Set and Get Method Action / / For this element without attributes and element sub-nodes, only the elements of the text sub-node, can be simply processed // serv .addsimple Child (ElementObject.single, "servlet-name", string.class, "name"); // This element is a sub-node of servlet, so adds to the SERV object, not to DOC, // The processing of a parameter is the same as the servlet element. There is a property String Name and its set and get method in servletLabel.java. // This property can only be basic data type or string, string.class means its data type "Name" is the name of the property, // If it is the basic data type (int, long), the corresponding type is Integer.Type, long.type, etc., //, such as: addsimplechild (element.optional, "lable-name" , Integer.Type, "Name"); org.apache.struts.Action.ActionServlet

//serv.addsimplechild (ElementObject.single ,"servlet-class", scring.class ,"servletclass "); // with servlet-name debug 2 config /web-inf/struts-config.xml 2 action *. do /web-inf/struts-bean.tld / web-inf /struts-bean.tld /web-inf/struts-html.TLD / web-inf / Struts-html.tld program: WebApplicationLabel.java

Public class WebApplicationLabel {// must be public class private servletlabel [] servlets; private servletmapping [] map; private taglib [] tags;

public WebApplicationLabel () {} // must have a public no-argument constructor type public void setServlets (ServletLabel [] servlets) {this.servlets = servlets;} public ServletLabel [] getServlets () {return servlets;} // get the other And the SET method is not listed, the following

}

Public class servletlabel {private string name; private string servletclass; private arraylist params = new arraylist (); private int startup;

public ServletLabel () {} public void setName (String name) {this.name = name;} public String getName () {return name;} public void setServletClass (String servletClass) {this.servletClass = servletClass;} public String getServletClass ( {RETURN servletclass;} public int getStartup () {return startup;} // What to define an integer type variable, which is an optional public void setStartup (int Startup) {Return Startup;} public servletparam [] getParams () {ServletParam [] rs = new servletparam [params.size ()]; return (servletParam []) params.toArray (RS);} / Note Here, do not need to define variable servletparam [] params, as long as you have Get and Set Methods Public void setParams (servletParam [] RS) {for (INT i = 0; I

}

Public class servletmapping {private string name; private String Url; public servletmapping () {} // set and get method

}

Public class tablib {private string uri; private tablib () {} // set and get method

}

The following is a test method:

Note: The following is not fully defined for the web.xml file, which can only analyze the nodes listed above for the general web.xml file.

Public class test () {// Using static variables and blocks are to speed up the second to XML file analysis.

You can get a test in the main method, then Copy to Static block private static documentObject doc = new documentObject ("web-app", webapplicationlabel.class; // Root Element Static {ElementObject Serv = Doc.Addchild ( ElementObject.multi, "servlets", servletLabel.class, "servlets"); // servlet element serv.addsimplechild (elementObject.single, "servlet-name", string.class, "name"); // servlet's NAME Element Serv.addsImplechild (ElementObject.single, "servlet-class", string.class, "servletclass"); // servlet-class element in // servlet, pay attention to case-sever serv.addsimplechild (ElementObject.Multi, "init-param" , ServletParam.class, "params"); //r in the init-param element in the servlet, it is 0 to N serv.addsimplechild (ElementObject.Option, "Load-on-Startup", Integer.Type, "Startup") ///// servlet's load-on-startup element, it is an optional INT type. ElementObject map = doc.addchild (ElementObject.Multi, "servlet-mapping", servletMapping.class, "maps"); map.addsimplechild (ElementObject.single, "servlet-name", string.class, "name"); map.addsimplechild (elementObject.single, "url-pattern", string.class, "url" ); ElementObject tag = doc.addChild (ElementObject.MULTI, "taglib", ServletLabel.class, "servlets"); map.addSimpleChild (ElementObject.SINGLE, "taglib-uri", String.class, "uri"); map .addsimplechild (ElementObject.single, "taglib-location", string.class, "location"); / * If the element servlet has one or more attributes, this is handled: serv.addattribute (String Label, Class Type, String Property Boolean Required) Label is the property name, TYPE is type (only basic type or string),

Property is the property name in servletLabel.java, and request means if it must be required if the element servlet contains the text node to process (eg org.apache.struts.Action .ActionServlet ) serv.addTextProperty (String textProperty, Class textType, boolean required) textProperty ServletLabel.java it in the attribute name, type is the type (or types can be substantially String), required if it refers to It must be noted that this method will not be called again to call the method addsimplechild and addchild methods, and vice versa. * /} Public static void main (String [] args) {WebApplicationLabel App = (WebApplicationLabel) Doc.read (New File ("D: /1.xml")); // Read file Doc.write (App, New FILE ("D: /2.xml")); // Write to XML file / * When writing a file, if an element is defined as ElementObject.single, but its correspondence value is null, the error is defined as ElementObject.Option However, its correspondence value is null, and does not define as ElementObject.morethanone (more than one), but its corresponding value is null, the error is defined as ElementObject.Multi, but its correspondence is null, will not read When you take the file, you will ignore all nodes defined in the DOC defined as ElementObject.single, but this element does not have this element in the file, the error is defined as ElementObject.Option, but this element does not have this element, it is defined as ElementObject.morethanone (More than one), but this element does not have this element, the error is defined as ElementObject.multi, but this element does not have this element, no error * /}}

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

New Post(0)