dom4j 操作xml

xiaoxiao2021-02-28  54

引入包

<dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId> <version>1.6.1</version> </dependency> 对xml进行增删改查:

package com.test.acount.acount.persist; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStreamWriter; import java.io.Writer; import java.util.List; import java.util.Properties; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentFactory; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; import org.springframework.context.support.ClassPathXmlApplicationContext; public class AccountPersistServiceImpl implements AccountPersistService { private static final String ElEMENT_ROOT="account-persist"; private static final String ElEMENT_ACCOUNTS="accounts"; private static final String ElEMENT_ACCOUNT="account"; private static final String ELEMENT_ACCOUNT_ID="id"; private static final String ELEMENT_ACCOUNT_NAME="name"; private static final String ELEMENT_ACCOUNT_EMAIL="email"; private static final String ELEMENT_ACCOUNT_PASSWORD="password"; private static final String ELEMENT_ACCOUNT_ACTIVATED="activated"; private String filePath; //spring注入 private SAXReader reader=new SAXReader(); public String getFilePath() { return filePath; } public void setFilePath(String filePath) { this.filePath = filePath; } private Document readDocument() throws AcountPersistException{ File dataFile = new File(filePath); if(!dataFile.exists()){ System.out.println(dataFile); dataFile.getParentFile().mkdirs(); Document doc = DocumentFactory.getInstance().createDocument(); Element rootEle = doc.addElement(ElEMENT_ROOT); rootEle.addElement(ElEMENT_ACCOUNTS); writeDocument(doc); } try { return reader.read(new File(filePath)); } catch (DocumentException e) { throw new AcountPersistException("unable to read persist data xml", e); } } private void writeDocument(Document doc) throws AcountPersistException{ Writer out=null; try { out=new OutputStreamWriter(new FileOutputStream(filePath)); XMLWriter writer = new XMLWriter(out, OutputFormat.createPrettyPrint()); writer.write(doc); } catch (IOException e) { throw new AcountPersistException("unable to write persist data xml", e); }finally{ if(out!=null){ try { out.close(); } catch (IOException e) { throw new AcountPersistException("unable to close persist data xml writer", e); } } } } //创建账户 public Account createAccount(Account account) throws AcountPersistException { //先检查ID是否唯一,不唯一不操作 Document doc = readDocument(); Element accountsEle = doc.getRootElement().element(ElEMENT_ACCOUNTS); List<Element> accountEleList = accountsEle.elements(); for(Element e:accountEleList){ if(e.elementText((ELEMENT_ACCOUNT_ID)).equals(account.getId())){ //已经存在 System.out.println("新增失败,该账户已经存在"); return null; } } Element accountEle = DocumentHelper.createElement(ElEMENT_ACCOUNT); Element idEle = DocumentHelper.createElement(ELEMENT_ACCOUNT_ID); idEle.setText(account.getId()); accountEle.add(idEle); Element nameEle = DocumentHelper.createElement(ELEMENT_ACCOUNT_NAME); nameEle.setText(account.getName()); accountEle.add(nameEle); Element emailEle = DocumentHelper.createElement(ELEMENT_ACCOUNT_EMAIL); emailEle.setText(account.getEmail()); accountEle.add(emailEle); Element passwordEle = DocumentHelper.createElement(ELEMENT_ACCOUNT_PASSWORD); passwordEle.setText(account.getPassword()); accountEle.add(passwordEle); Element actEle = DocumentHelper.createElement(ELEMENT_ACCOUNT_ACTIVATED); actEle.setText(String.valueOf(account.getActive())); accountEle.add(actEle); accountEleList.add(accountEle); writeDocument(doc); System.out.println("新增成功"); return account; } //查询账户 public Account readAccount(String id) throws AcountPersistException { Document doc = readDocument(); Element accountsEle = doc.getRootElement().element(ElEMENT_ACCOUNTS); for(Element accountEle:(List<Element>)accountsEle.elements()){ if(accountEle.elementText(ELEMENT_ACCOUNT_ID).equals(id)){ return buildAccount(accountEle); } } return null; } private Account buildAccount(Element element){ Account account = new Account(); account.setId(element.elementText(ELEMENT_ACCOUNT_ID)); account.setName(element.elementText(ELEMENT_ACCOUNT_NAME)); account.setPassword(element.elementText(ELEMENT_ACCOUNT_PASSWORD)); account.setEmail(element.elementText(ELEMENT_ACCOUNT_EMAIL)); account.setActivated("true".equals(element.elementText(ELEMENT_ACCOUNT_ACTIVATED))?true:false); return account; } //更新账户 public Account updateAccount(Account account) throws AcountPersistException { Document doc = readDocument(); Element accountsEle = doc.getRootElement().element(ElEMENT_ACCOUNTS); String id=account.getId(); for(Element accountEle:(List<Element>)accountsEle.elements()){ if(accountEle.elementText(ELEMENT_ACCOUNT_ID).equals(id)){ accountEle.element(ELEMENT_ACCOUNT_NAME).setText(account.getName()); accountEle.element(ELEMENT_ACCOUNT_EMAIL).setText(account.getEmail()); accountEle.element(ELEMENT_ACCOUNT_PASSWORD).setText(account.getPassword()); accountEle.element(ELEMENT_ACCOUNT_ACTIVATED).setText(String.valueOf(account.getActive())); break; } } writeDocument(doc); System.out.println("更新成功"); return account; } //删除账户 public void deleteAccount(String id) throws AcountPersistException { Document doc = readDocument(); Element accountsEle = doc.getRootElement().element(ElEMENT_ACCOUNTS); List<Element> accountsList = accountsEle.elements(); for(Element accountEle:accountsList){ if(accountEle.elementText(ELEMENT_ACCOUNT_ID).equals(id)){ accountsList.remove(accountEle); break; } } writeDocument(doc); System.out.println("删除成功"); } public static void main(String[] args) throws AcountPersistException { ClassPathXmlApplicationContext application = new ClassPathXmlApplicationContext("acount-persist.xml"); AccountPersistService accountPersistService = (AccountPersistService) application.getBean("accountPersistService"); // Account account = new Account(); // account.setId("ls"); // account.setName("lisi"); // account.setEmail("lisi@163.com"); // account.setPassword("123123"); // account.setActivated(true); // Account createAccount = accountPersistService.createAccount(account); // System.out.println(createAccount); // Account account = new Account(); // account.setName("zhangsan");; // account.setId("ls"); // account.setEmail("zhangsan@163.com"); // account.setPassword("123456"); // account.setActivated(false); // accountPersistService.updateAccount(account); // Account readAccount = accountPersistService.readAccount("ls"); System.out.println(readAccount); // accountPersistService.deleteAccount("ls"); } }

转载请注明原文地址: https://www.6miu.com/read-41911.html

最新回复(0)