Clob is saved as a local xml file. After modification, it is uploaded. clobxml

Source: Internet
Author: User

Clob is saved as a local xml file. After modification, it is uploaded. clobxml

I wrote a small program with my friends over the past two days. The functions are as follows:

First, save the database clob as a local xml file, modify the xml file, and upload it to the database.

The main difficulties are as follows:

1: clob File Download and upload, which save as local file requirements is UTF-8 format

2: Modify nodes in the xml file


Clob upload and download are as follows:

Import java. io. bufferedReader; import java. io. fileInputStream; import java. io. fileNotFoundException; import java. io. fileOutputStream; import java. io. IOException; import java. io. inputStreamReader; import java. io. outputStreamWriter; import java. io. reader; import java. io. unsupportedEncodingException; import java. SQL. clob; import java. SQL. connection; import java. SQL. preparedStatement; import java. SQL. resultSet; imp Ort java. SQL. SQLException; import java. SQL. statement; /*** @ author GuoDi-ct dc **/public class ClobModify {/*** @ param id ID id * in the database returns the absolute path of the saved file */public static String ClobToXml (int id) {Connection conn = DB. getConn (); Statement stmt = DB. createStmt (conn); String SQL = "select * from BD_PROCESS_DEF_VER where ID =" + id; ResultSet rs = DB. getRs (stmt, SQL); String xmlFile = null; try {if (rs. next () {int Fjbh = rs. getInt (1); xmlFile = "d: \ xml \" + fjbh + ". xml "; Clob clob = rs. getClob (16); FileOutputStream fo = new FileOutputStream (xmlFile); outputstream writer so = new OutputStreamWriter (fo, "UTF-8"); if (clob! = Null) {Reader is = clob. getCharacterStream (); BufferedReader br = new BufferedReader (is); String s = br. readLine (); while (s! = Null) {so. write (s + System. getProperty ("line. separator "); // System. out. println (str); s = br. readLine () ;}} so. flush (); so. close () ;}} catch (SQLException | IOException e) {e. printStackTrace ();} DB. close (rs); DB. close (stmt); DB. close (conn); return xmlFile;} public static void updateClob (String fileName, int id) {FileInputStream FD = null; InputStreamReader rd = null; try {FD = new FileInputStream (fileName); Rd = new InputStreamReader (FCM, "UTF-8");} catch (FileNotFoundException e2) {e2.printStackTrace ();} catch (UnsupportedEncodingException e) {e. printStackTrace ();} PreparedStatement pst = null; Connection conn = DB. getConn (); Statement stmt = DB. createStmt (conn); try {conn. setAutoCommit (false);} catch (SQLException e1) {e1.printStackTrace ();} String sql1 = "update BD_PROCESS_DEF_VER s set s. NODE_INFO =' 'Where ID = "+ id; // an empty field needs to be set here, and no NULL pointer try retry stmt.exe cute (sql1);} catch (SQLException e) {e. printStackTrace ();} String sql2 = "select * from BD_PROCESS_DEF_VER s where s. ID = "+ id; // lock the data row for update. Note that the" for update "Statement ResultSet rs = null; try {rs = stmt.exe cuteQuery (sql2 );} catch (SQLException e) {e. printStackTrace ();} try {while (rs. next () {String sql3 = "update BD_PROCESS_DEF_VER set NODE_INFO =? Where ID = "+ id; pst = conn. prepareStatement (sql3); pst. setCharacterStream (1, rd, please submit your own pst.exe cuteUpdate ();} // submit the conn in the last step. commit (); conn. setAutoCommit (true);} catch (SQLException e) {e. printStackTrace ();} finally {DB. close (rs); DB. close (stmt); try {pst. close ();} catch (SQLException e) {e. printStackTrace ();} DB. close (conn );}}}

Where DB is the javabean connecting to the database, as follows:

Import java. SQL. connection; import java. SQL. driverManager; import java. SQL. resultSet; import java. SQL. SQLException; import java. SQL. statement; /*** @ author GuoDi-ct dc * jdcbc JavaBean **/public class DB {// The driver is public in the JAR package of the JDBC driver configured in classpath. static final String DBDRIVER = "oracle. jdbc. driver. oracleDriver "; // The Connection address is provided by each database manufacturer separately. Therefore, remember public static final String DBURL =" jdbc: oracle: thin: @ 1 72.17.0000215: 1521: BPMIDE "; // username used to connect to the database public static final String DBUSER =" bpmuser "; // password used to connect to the database public static final String DBPASS =" bpmd "; public static Connection getConn () {Connection conn = null; try {Class. forName (DBDRIVER); conn = DriverManager. getConnection (DBURL, DBUSER, DBPASS); // 2. Connect to the database} catch (ClassNotFoundException e) {e. printStackTrace ();} // 1. Use the CLASS to load the driver catch (SQLException e) {e. PrintStackTrace () ;}return conn;} public static Statement createStmt (Connection conn) {Statement stmt = null; try {stmt = conn. createStatement ();} catch (SQLException e) {e. printStackTrace () ;}return stmt;} public static ResultSet getRs (Statement stmt, String SQL) {ResultSet rs = null; try {rs = stmt.exe cuteQuery (SQL );} catch (SQLException e) {e. printStackTrace ();} return rs;} public static void close (Res UltSet rs) {if (rs! = Null) {try {rs. close ();} catch (SQLException e) {e. printStackTrace () ;}finally {rs = null ;}} public static void close (Statement stmt) {if (stmt! = Null) {try {stmt. close ();} catch (SQLException e) {e. printStackTrace () ;}finally {stmt = null ;}} public static void close (Connection conn) {if (conn! = Null) {try {conn. close ();} catch (SQLException e) {// TODO Auto-generated catch blocke. printStackTrace () ;}finally {conn = null ;}}}}


The xml modification procedure is as follows:

Import java. io. fileNotFoundException; import java. io. fileOutputStream; import java. io. IOException; import javax. xml. parsers. documentBuilder; import javax. xml. parsers. documentBuilderFactory; import javax. xml. parsers. parserConfigurationException; import javax. xml. transform. outputKeys; import javax. xml. transform. transformer; import javax. xml. transform. transformerFactory; import javax. xml. transform. dom. DOMSource; import javax. xml. transform. stream. streamResult; import org. w3c. dom. document; import org. w3c. dom. element; import org. w3c. dom. nodeList; import org. xml. sax. SAXException;/***** @ author zhangwen. ctdc DOM update and resolution XML Document */public class XmlAnalysis/* implements XmlDocumentInterface */{private document Document document; public void init () {try {DocumentBuilderFactory factory = DocumentBuilderFactory. newInstance (); DocumentBuilder builder = factory.newdocumentbuilder({this.doc ument = builder. newDocument ();} catch (ParserConfigurationException e) {System. out. println (e. getMessage () ;}} public void insertElementNode (String fileName, String nodeName, String newElementName) {document = parserXml (fileName); NodeList nodeList = document. getElementsByTagName (nodeName); for (int I = 0; I <nodeList. getLength (); I ++) {Element element = document. createElement (newElementName); nodeList. item (I ). appendChild (element);} createXml (fileName);} public void insertAttrNode (String fileName, String nodeName, String newAttrName, String attrValue) {document = parserXml (fileName ); nodeList nodeList = document. getElementsByTagName (nodeName); for (int I = 0; I <nodeList. getLength (); I ++) {Element element = (Element) (nodeList. item (I); element. setAttribute (newAttrName, attrValue);} createXml (fileName);} public void insertTextNode (String fileName, String nodeName, String textNode) {document = parserXml (fileName); NodeList nodeList = document. getElementsByTagName (nodeName); for (int I = 0; I <nodeList. getLength (); I ++) {nodeList. item (I ). appendChild (document. createTextNode (textNode);} createXml (fileName);} public void deleteElementNode (String fileName, String nodeName) {document = parserXml (fileName); NodeList nodeList = document. getElementsByTagName (nodeName); while (nodeList. getLength ()> 0) {nodeList. item (0 ). getParentNode (). removeChild (nodeList. item (0); nodeList = document. getElementsByTagName (nodeName);} createXml (fileName);} public void updateNode (String fileName, String nodeName, String attrName, String newAttrValue) {document = parserXml (fileName ); nodeList nodeList = document. getElementsByTagName (nodeName); for (int I = 0; I <nodeList. getLength (); I ++) {Element node = (Element) nodeList. item (I); node. setAttribute (attrName, newAttrValue);} createXml (fileName);} private Document parserXml (String fileName) {try {DocumentBuilderFactory dbf = DocumentBuilderFactory. newInstance (); DocumentBuilder db = dbf. newDocumentBuilder (); Document document = db. parse (fileName); System. out. println ("---------------------------------" + "Resolution completed" + "------------------------------------------"); return document;} catch (FileNotFoundException e) {System. out. println (e. getMessage ();} catch (ParserConfigurationException e) {System. out. println (e. getMessage ();} catch (SAXException e) {System. out. println (e. getMessage ();} catch (IOException e) {System. out. println (e. getMessage ();} return document;} private void createXml (String fileName) {try {/** write the content in the document to the file */TransformerFactory tFactory = TransformerFactory. newInstance (); Transformer transformer = tFactory. newTransformer (); transformer. setOutputProperty (OutputKeys. ENCODING, "UTF-8"); transformer. setOutputProperty (OutputKeys. INDENT, "yes"); DOMSource source = new DOMSource (document); StreamResult result = new StreamResult (new FileOutputStream (fileName); transformer. transform (source, result); System. out. println ("------------------------------" + "XML file updated successfully" + "---------------------------------------");} catch (Exception exception) {System. out. println ("Update" + fileName + "error:" + exception); exception. printStackTrace ();}}}

The interfaces and descriptions provided by the program are as follows:

/*** @ Author GuoDi and ZhangWen **/public interface NodeInfoInterface {/*** Insert elements to the node in the XML file * @ param time * @ param nodeName tag name * @ param newElementName new label */public void insertElementNode (String time, string nodeName, String newElementName ); /*** @ param time * @ param nodeName tag name * @ param newAttrName new attribute name * @ param attrValue new attribute value * XML document insertion attribute node */public void insertAttrNode (String time, string nodeName, String newAttrName, String attrValue ); /*** @ param time * @ param nodeName tag name * @ param textNode text * XML file insertion text node */public void insertTextNode (String time, String nodeName, string textNode);/*** @ param time * @ param nodeName tag name * Delete all corresponding element nodes in the XML document */public void deleteElementNode (String time, String nodeName ); /*** @ param time * @ param nodeName tag name * @ param newAttrName new attribute name * @ param attrValue new attribute value * XML document modifying attribute node content */public void updateNode (string time, string nodeName, String newAttrName, String attrValue );}

















. Net: Why can't I save the xml file? on the local machine, I can create and save the xmlDocSave file. However, after uploading the project to the server, it won't work.

There are many situations
First, your permissions are insufficient. However, if you have been working in the company for a long time, this permission problem does not exist. Otherwise, you may have discovered it before.

Another problem is the path of your XML file. Check whether the path in your configuration file is changed or whether the saving path of the Code settings in your program fails.

The Windows 7 xml file cannot be saved after modification

Copy the file to a non-system disk.
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.