溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Java使用DOM4j實(shí)現(xiàn)讀寫XML文件的屬性和元素

發(fā)布時間:2020-08-27 09:18:20 來源:腳本之家 閱讀:124 作者:程沐喆 欄目:編程語言

DOM4可以讀取和添加XML文件的屬性或者元素

讀取屬性:

public static void ReadAttributes() throws DocumentException {
  File file = new File("D:\\cmz\\java\\XMLTest\\Customertest.xml");
  SAXReader reader = new SAXReader();
  Document doc = reader.read(file);
  Element root = doc.getRootElement();
  try {

   for (Iterator iterator = root.elementIterator(); iterator.hasNext();) {
    Element element = (Element) iterator.next();
    String CustomerID = element.attributeValue("CustomerID");
    System.out.println("CustomerID = " + CustomerID);
    String CompanyName = element.attributeValue("CompanyName");
    System.out.println("CompanyName = " + CompanyName);
    System.out.println("ContactName = "
      + element.attributeValue("ContactName"));
    System.out.println("ContactTitle = "
      + element.attributeValue("ContactTitle"));
    System.out.println("Address = "
      + element.attributeValue("Address"));
    System.out.println("City = " + element.attributeValue("Cit阿y"));
    System.out.println("PostalCode = "
      + element.attributeValue("PostalCode"));
    System.out.println("Country = "
      + element.attributeValue("Country"));
    System.out
      .println("Phone = " + element.attributeValue("Phone"));
    System.out.println("Fax = " + element.attributeValue("Fax"));
    System.out
      .println("--------------------------------------------------------\t");

   }
  } catch (Exception e) {
   // TODO: handle exception
  }

 }

讀取元素:

public static void ReadInnerTest() throws DocumentException {
  File file = new File("D:\\cmz\\java\\XMLTest\\Customer1.xml");
  SAXReader reader = new SAXReader();
  Document doc = reader.read(file);
  Element root = doc.getRootElement();

  try {
   for (Iterator iterator = root.elementIterator(); iterator.hasNext();) {
    Element type = (Element) iterator.next();
    
    System.out.println(type.elementText("CustomerID"));
    System.out.println(type.elementText("CompanyName"));
    System.out.println(type.elementText("ContactName"));
    System.out.println(type.elementText("ContactTitle"));
    System.out.println(type.elementText("Address"));
    System.out.println(type.elementText("City"));
    System.out.println(type.elementText("PostalCode"));
    System.out.println(type.elementText("Country"));
    System.out.println(type.elementText("Phone"));
    System.out.println(type.elementText("Fax"));
    System.out.println("---------------------------------\t");
   }

  } catch (Exception e) {
   // TODO: handle exception
  }
 }

寫入屬性:

public static void WriteAttributes() {
  Document doc = DocumentHelper.createDocument();
  Element ele = doc.addElement("table");
  for (int i = 1; i < 5; i++) {
   Element Customers = ele.addElement("Customers");
   
   Customers.addAttribute("CustomerID", "ALFKI" + i);
   Customers.addAttribute("CompanyName", "Alfreds Futterkiste" + i);
   Customers.addAttribute("ContactName", "Maria Anders" + i);
   Customers.addAttribute("ContactTitle", "Sales Representative" + i);
   Customers.addAttribute("Address", "Obere Str. 57");
   Customers.addAttribute("City", "beijin");
   Customers.addAttribute("PostalCode", "12209");
   Customers.addAttribute("Country", "Germany");
   Customers.addAttribute("Phone", "030-0074321");
   Customers.addAttribute("Fax", "030-0076545");
   try {
    XMLWriter writer = new XMLWriter(new FileWriter(new File(
      "Customertest.xml")));
    writer.write(doc);
    writer.close();
   } catch (Exception e) {
    // TODO: handle exception
   }
  }

 }

寫出元素:

public static void writeInnerTest(){
  Document doc = DocumentHelper.createDocument();
  Element ele = doc.addElement("table");
  
  for (int i = 1; i < 5; i++) {
   Element Customers = ele.addElement("row");
   Element CustomerID = ele.addElement("CustomerID");
   CustomerID.setText("ALFKI" + i);
   Element CompanyName = ele.addElement("CompanyName");
   CompanyName.setText("Alfreds Futterkiste" + i);
   Element ContactName = ele.addElement("ContactName");
   ContactName.setText("Maria Anders" + i);
   Element ContactTitle = ele.addElement("ContactTitle");
   ContactTitle.setText("Sales Representative" + i);
   Element Address = ele.addElement("Address");
   Address.setText("Obere Str. 57");
   Element City = ele.addElement("City");
   City.setText("beijin");
   Element PostalCode = ele.addElement("PostalCode");
   PostalCode.setText("12209");
   Element Country = ele.addElement("Country");
   Country.setText("Germany");
   Element Phone = ele.addElement("Phone");
   Phone.setText("030-0074321");
   Element Fax = ele.addElement("Fax");
   Fax.setText("030-0076545");
  }
  try {
   XMLWriter writer = new XMLWriter(new FileWriter(new File(
     "Customertest2.xml")));
   writer.write(doc);
   writer.close();
  } catch (Exception e) {
   // TODO: handle exception
  }
  
  
 }

可以修改屬性的文本內(nèi)容:

public static void ReadUpdateattribute() throws DocumentException{
  File file = new File("D:\\cmz\\java\\XMLTest\\Customertest.xml");
  SAXReader reader = new SAXReader();
  Document doc = reader.read(file);
  Element root = doc.getRootElement();
  
  try {

   for (Iterator iterator = root.elementIterator("Customers"); iterator.hasNext();) {
    Element element = (Element) iterator.next();
    String name = "ALFKI1";
    if (name.equals(element.attributeValue("CustomerID")) ) {
     Attribute attr = element.attribute("CustomerID");
     attr.setValue("234");
     Element ContactName = element.addElement("23424");
     ContactName.setText("676767" );
    }
        

   }
   XMLWriter writer = new XMLWriter(new FileOutputStream(file));
   writer.write(doc);
   ReadAttributes();
  } catch (Exception e) {
   // TODO: handle exception
  }
  
 }

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI