In the project, we use xml files a lot, whether it is parameter configuration or data interaction with other systems.
Today we will talk about using dom4j in Java to operate XML files.
The packages we need to introduce:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileWriter;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
|
Copy after login
1. Convert the content of the XML file into String
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
public static String doc2String(Document document)
{
String s = "";
try
{
ByteArrayOutputStream out = new ByteArrayOutputStream();
OutputFormat format = new OutputFormat(" ", true, "GB2312");
XMLWriter writer = new XMLWriter(out, format);
writer.write(document);
s = out.toString("GB2312");
} catch (Exception ex)
{
ex.printStackTrace();
}
return s;
}
|
Copy after login
2. Convert the String that conforms to the XML format into XML Document
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
public static Document string2Document(String s)
{
Document doc = null;
try
{
doc = DocumentHelper.parseText(s);
} catch (Exception ex)
{
ex.printStackTrace();
}
return doc;
}
|
Copy after login
3. Save the Document object as an xml file locally
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
public static boolean doc2XmlFile(Document document,String filename)
{
boolean flag = true;
try
{
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("GB2312");
XMLWriter writer = new XMLWriter( new FileWriter( new File(filename)),format);
writer.write(document);
writer.close();
} catch (Exception ex)
{
flag = false;
ex.printStackTrace();
}
return flag;
}
|
Copy after login
4. Save the string in xml format as a local file. If the string format does not comply with xml rules, failure will be returned
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
public static boolean string2XmlFile(String str,String filename)
{
boolean flag = true;
try
{
Document doc = DocumentHelper.parseText(str);
flag = doc2XmlFile(doc,filename);
} catch (Exception ex)
{
flag = false;
ex.printStackTrace();
}
return flag;
}
|
Copy after login
5. Load an xml document
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
public static Document load(String filename)
{
Document document = null;
try
{
SAXReader saxReader = new SAXReader();
document = saxReader.read( new File(filename));
}
catch (Exception ex){
ex.printStackTrace();
}
return document;
}
|
Copy after login
6. Demonstrate saving a String as an xml file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
public void xmlWriteDemoByString()
{
String s = "";
s = "<config>\r\n"
+" <ftp name='DongDian'>\r\n"
+" <ftp-host>127.0.0.1</ftp-host>\r\n"
+" <ftp-port>21</ftp-port>\r\n"
+" <ftp-user>cxl</ftp-user>\r\n"
+" <ftp-pwd>longshine</ftp-pwd>\r\n"
+" <!-- ftp最多尝试连接次数 -->\r\n"
+" <ftp- try >50</ftp- try >\r\n"
+" <!-- ftp尝试连接延迟时间 -->\r\n"
+" <ftp-delay>10</ftp-delay>\r\n"
+" </ftp>\r\n"
+"</config>\r\n";
string2XmlFile(s,"xmlWriteDemoByString.xml");
string2XmlFile(s,"classes/xmlWriteDemoByString.xml");
}
|
Copy after login
7. Demonstrate manually creating a Document and save it as an XML file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
public void xmlWriteDemoByDocument()
{
Document document = DocumentHelper.createDocument();
Element configElement = document.addElement("config");
configElement.addComment("东电ftp配置");
Element ftpElement = configElement.addElement("ftp");
ftpElement.addAttribute("name","DongDian");
Element hostElement = ftpElement.addElement("ftp-host");
hostElement.setText("127.0.0.1");
(ftpElement.addElement("ftp-port")).setText("21");
(ftpElement.addElement("ftp-user")).setText("cxl");
(ftpElement.addElement("ftp-pwd")).setText("longshine");
ftpElement.addComment("ftp最多尝试连接次数");
(ftpElement.addElement("ftp- try ")).setText("50");
ftpElement.addComment("ftp尝试连接延迟时间");
(ftpElement.addElement("ftp-delay")).setText("10");
doc2XmlFile(document,"classes/xmlWriteDemoByDocument.xml");
}
|
Copy after login
8. Demonstrate reading the value of a specific node in the file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
public static void xmlReadDemo()
{
Document doc = load("classes/xmlWriteDemoByDocument.xml");
List list = doc.selectNodes("/config/ftp" );
Iterator it = list.iterator();
while (it.hasNext())
{
Element ftpElement = (Element)it.next();
System.out.println("ftp_name="+ftpElement.attribute("name").getValue());
}
list = doc.selectNodes("/config/ftp/@name" );
it = list.iterator();
while (it.hasNext())
{
Attribute attribute = (Attribute)it.next();
System.out.println("@name="+attribute.getValue());
}
list = doc.selectNodes("/config/ftp/ftp-host" );
it = list.iterator();
Element hostElement=(Element)it.next();
System.out.println("DongDian's ftp_host="+hostElement. getText ());
}
|
Copy after login
9. Modify or delete a value or attribute
1 2 3 4 5 6 7 8 | ftpElement.remove(hostElement);
ftpElement.remove(nameAttribute);
hostElement.setText("192.168.0.1");
nameAttribute.setValue("ChiFeng");
|
Copy after login
The above is the content of dom4j operating xml files (full). For more related content, please pay attention to the PHP Chinese website (www.php.cn)!