Jdom study (3) read and write XML documents (transfer)

xiaoxiao2021-03-06  42

Take the Sample1.xml file as an example, let's see how to increase the capacity of "disk disk2" to 20G. 8G 200 1580 10g 500 3000 jive has a very famous class, XMLproperties.java, it Providing a function of the XML data source, the basic method is that the primary level of the root element to the child element recursive finds child elements, no matter how deep this child element, we can set, modify or delete it. . First, we use "." To separate the path from the root to the child element (not included) Disk.capacity, then we can use the "Disk DISK2" with the method of the XMLProperties class object. The capacity is increased to 20 g.

Public class test3 {public static void main (string args []) {xmlproperties xp = new xmlproperties ("Sample1.xml"); system.out.println (xp.getproperty ("Disk2.capacity"); xp.setproperty "Disk2.capacity", "20g"); System.out.Println ("OK"); system.out.println (xp.getproperty ("Disk2.capacity");}} Run: C: / Java> Java Test3 10g OK 20G Finally, please see: Xmlproperties.java (from jivejdon2.6, please use the jdom.jar compile operation) Import Java.IO. *; Import Java.util. *; import org.jdom *;. import org.jdom.input *;. import org.jdom.output *;. public class XMLProperties {private File file; private Document doc; private Map propertyCache = new HashMap (); public XMLProperties (String File) {this.file = new file (file); try {saxbuilder builder = new saxbuilder (); // dataunformatfilter format = new dataunformatfilter (); // builder.setXMLFILT ER (Format); DOC = Builder.build (New File (File));} Catch (Exception E) {System.err.Println ("Error Creating XML Parser IN" "PropertyManager.java); E.PrintStackTrace );}} public String getProperty (String name) {if (propertyCache.containsKey (name)) {return (String) propertyCache.get (name);} String [] propName = parsePropertyName (name); Element element = doc.getRootElement (); For (int i = 0; i

Element.getchild (propName [i]); if (element == null) {return null;}} string value = element.getText (); if ("" .Equals (value)) {Return null;} else {value = value.trim (); propertyCache.put (name, value); return value;}} public String [] getChildrenProperties (String parent) {String [] propName = parsePropertyName (parent); Element element = doc.getRootElement (); For (int i = 0; i

} Public void deleteProperty (String name) {String [] propName = parsePropertyName (name); Element element = doc.getRootElement (); for (int i = 0; i

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

New Post(0)