📄 xmlprocess.java
字号:
package autoencodeinfinit.XMLProcess;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import java.io.*;import java.util.Properties;import javax.xml.transform.*;import javax.xml.transform.dom.DOMSource;import javax.xml.transform.stream.StreamResult;import org.w3c.dom.*;/** * This class include the function of load in xml documents , write in xml documents * and append , modify ,remove any node of ths document. * @version 0.1 * @author ulysess */public class XMLProcess { private File docFile = null; private Document doc = null , newdoc = null; private Element root = null; private String orgiadd = ""; /** * Constructor, you need transport a parameter of the xml file address; * please note this constructor will ignore the wihlespace in the xml file! * @param fi the file address you want to load */ public XMLProcess(String fi) { try { docFile = new File(fi); this.orgiadd = fi; DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); doc = db.parse(docFile); doc.normalizeDocument(); doc.normalize(); } catch (javax.xml.parsers.ParserConfigurationException pce) { System.out.println("The parser was not configured correctly."); System.exit(1); } catch (java.io.IOException ie) { System.out.println("Cannot read input file."); System.exit(1); } catch (org.xml.sax.SAXException se) { System.out.println("Problem occured parsing the file."); System.exit(1); } catch (java.lang.IllegalArgumentException ae) { System.out.println("Please specify an XML source."); System.exit(1); } root = doc.getDocumentElement(); root.normalize(); } /** * return the DocumentElement of this xml * @return the root document element */ public Element getRoot() { System.out.println("The root element is " + "\t" +root.getNodeName() ); return root; } /** * return a NodeList of the children nodes of document element * @return the children list of root node */ public NodeList getChildNodes() { return this.root.getChildNodes(); } /** * return a NodeList of the children nodes of specified node and the index of * this node , if only have one ,you need set the index as 0; * @param nodename the element name * @param index the index of the element if there has only one ,it must be 0; * @return a NodeList of the children List */ public NodeList getChildNodes(String nodename,int index) { NodeList children = null; if ((children = this.doc.getElementsByTagName(nodename) ).getLength()>0) { children = children.item(index).getChildNodes(); } return children; } /** * set the value of a element * general you can get this by using getChildNodes("the name of element * which you want to change value,0).item(0).setNodeValue("value you want to set") * @param ele the element node you want to be set * @param value the value of this element */ public void setValue(Element ele,String value) { ele.getFirstChild().setNodeValue(value); } /** * insert a new element node and its value node as a last children node of "eleti" * if you need insert only a element ,transmit a null string as nodevalue * @param eleti the parent element node of the element you want to insert * @param nodename the name of the element you want to insert * @param nodevalue the value of the element you want to insert */ public Element insertNodeIn(Element eleti,String nodename,String nodevalue) { Element newele = doc.createElement(nodename); if(nodevalue != null) { Text elevalue = doc.createTextNode(nodevalue); newele.appendChild(elevalue); } return (Element) eleti.insertBefore(newele , null ); } /** * insert a new element node and its value node as a children node of "eleti" * before the children node "insertbefore" * @param eleti the parent element node of the element you want to insert * @param insertbefore the sibling element you want to insert before * @param nodename the name of the element you want to insert * @param nodevalue the value of the element you want to insert */ public Element insertNodeIn(Element eleti,Element insertbefore , String nodename,String nodevalue) { Element newele = doc.createElement(nodename); if(nodevalue != null) { Text elevalue = doc.createTextNode(nodevalue); newele.appendChild(elevalue); } return (Element) eleti.insertBefore(newele , insertbefore); } /** * remove all element node named rm from the ele node * @param ele the parent node of the element to be removed * @param rm the name of the element to be removed */ public void removeNode(Element ele,String rm) { ele.removeChild(ele.getElementsByTagName(rm).item(0).getFirstChild()); } /** * write the xml document in memory into the disk with address newadd * @param newadd if newadd is null this method will flush the orginal xml document */ public void WriteDocument(String newadd) { if (newadd == null) { this.WriteDocument(this.orgiadd); } else { DOMSource doms = new DOMSource(this.doc); File outf = new File(newadd); StreamResult sr = new StreamResult(outf); try { TransformerFactory tf=TransformerFactory.newInstance(); Transformer t=tf.newTransformer (); //call the method transform() of Transformer object (XSLT engine) Properties properties = t.getOutputProperties(); properties.setProperty(OutputKeys.ENCODING,"UTF-8"); t.setOutputProperties(properties); t.transform(doms, sr); } catch (TransformerConfigurationException tce) { System.out.println("Transformer Configuration Exception\n"); tce.printStackTrace(); } catch (TransformerException te) { System.out.println ("Transformer Exception\n"); te.printStackTrace (); } } } /** * create a new XMLFile in the specify location * @param add * @return a new XMLProcess object */ public static XMLProcess CreateXMLFile(String add){ File fi = null; FileWriter fw = null; XMLProcess newxmlfile = null; try { fi = new File(add); if(fi.exists()) { System.out.println("File exist"); return null; } else { if( !fi.createNewFile() ) { System.out.println("create file failed"); return null; } } fw = new FileWriter(fi); fw.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?><root></root>"); fw.flush(); fw.close(); } catch (IOException e) { System.out.println("Exception :" + e); } newxmlfile = new XMLProcess(add); return newxmlfile; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -