Modify XML files in JAVA

Source: Internet
Author: User

Package com. cnten. test;


Import java. io. File;
Import java. io. FileNotFoundException;
Import java. io. FileOutputStream;
Import java. io. IOException;
Import java. io. UnsupportedEncodingException;
Import java. util. ArrayList;
Import java. util. Iterator;
Import java. util. List;


Import org. dom4j. Document;
Import org. dom4j. extends entexception;
Import org. dom4j. Element;
Import org. dom4j. io. SAXReader;
Import org. dom4j. io. XMLWriter;
Import org. junit. Test;
 
Public class XmlUtil {

/**
* @ Param args
*/
Public static void main (String [] args ){
List <String> columns = new ArrayList <String> (); // you can copy a chunk or a chunk.
Columns. add ("Arden ");
Columns. add ("Pinco ");
Columns. add ("Denis ");
// New XmlUtil (). testAddElements (columns); // you can copy everything, copy everything, and then copy everything.
New XmlUtil (). testUupdateElements (); // you can copy a pair of chunks.
 
}

 


/**
* Modify
*/
@ Test
Public void testUupdateElements (){
Try {

String path1 = "../CntenSecurity/res/values/theme. xml"; // CntenSecurity/res/values/theme. xml
Document baseXML1 = getDocument (path1 );
UpdateElementByName (baseXML1, "item", "android: textSize", "33sp ");
WriteXml (baseXML1, path1 );




/* String path2 = "./src/com/cnten/test. xml ";
Document baseXML2 = getDocument (path2 );
UpdateElementByName (baseXML2, "report_content", "start_row", "2013-04-06 ");
WriteXml (baseXML2, path2 );*/


} Catch (incluentexception e ){
E. printStackTrace ();
} Catch (IOException e ){
E. printStackTrace ();
}
}

/**
*
* @ Param document
* @ Param elementName
* @ Param attributeName
* @ Param newValue
*/
@ SuppressWarnings ("deprecation ")
Private void updateElementByName (Document document, String elementName, String attributeName, String newValue ){
Element root = document. getRootElement ();
System. err. println (root. element ("style"). elements (elementName). size ());

// Element updatedNode1 = root. element ("blackTheme ");
// Element updatedNode = (Element) root. element ("style"). elements (elementName). get (1); // parent element.
// UpdatedNode. setText ("aaa ");
// UpdatedNode. setAttributeValue (attributeName, newValue );


Iterator it = root. element ("style"). elementIterator (elementName );
While (it. hasNext ()){
Element request = (Element) it. next ();
If (request. attributeValue ("name"). equals (attributeName )){
Request. setText (newValue );
}
}


}

/**
*
* @ Param document
* @ Param columns
*/
Private void addElement (Document document, List <String> columns ){
Element root = document. getRootElement ();
Element updatedNode = root. element ("report_content"); // parent element.

For (int I = 0; I <columns. size (); I ++ ){
String column = columns. get (I );
Element newElement = updatedNode. addElement ("property"); // child element.
NewElement. addAttribute ("row_name", column); // the first property.
NewElement. addAttribute ("row_id", String. valueOf (I); // the second property.
}
}

/**
*
* @ Param document
* @ Param filePath
* @ Throws IOException
*/
Private void writeXml (Document document, String filePath) throws IOException {
File file = new File (filePath );
XMLWriter writer = null;
Try {
If (file. exists ())
{
File. delete ();
}
Writer = new XMLWriter (new FileOutputStream (file ));
Writer. write (document );
Writer. close ();
} Catch (UnsupportedEncodingException e ){
E. printStackTrace ();
} Catch (FileNotFoundException e ){
E. printStackTrace ();
} Catch (IOException e ){
E. printStackTrace ();
} Finally {
If (writer! = Null)
{
Writer. close ();
}
}
}

 

/**
*
* @ Param columns
*/
Private void testAddElements (List <String> columns ){
Try {
Document baseXML = getDocument ("./src/com/cnten/test/theme. xml"); // copy the original XML example successfully.

AddElement (baseXML, columns );

WriteXml (baseXML, "./src/com/cnten/test/theme. xml"); // duplicate ML duplicate copy
} Catch (incluentexception e ){
E. printStackTrace ();
} Catch (IOException e ){
E. printStackTrace ();
}
}

/**
*
* @ Param filePath
* @ Return
* @ Throws define entexception
*/
Private Document getDocument (String filePath) throws into entexception {
File file = new File (filePath );
SAXReader reader = new SAXReader ();

Return reader. read (file );
}


}

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.