xml文件的增删改查

本文详细介绍了如何使用Java进行XML文件的操作,包括如何增加新的元素,删除指定节点,修改文件内容以及查询XML数据。同时,还分享了一种简化XML操作的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

xml文件

<?xml version="1.0" encoding="gbk"?>

<books> 
  <book bookId="1475"> 
    <bookName>4568</bookName>  
    <price>1000.0</price> 
  </book>  
  <book bookId="159"> 
    <bookName>147</bookName>  
    <price>145.0</price> 
  </book>  
  <book bookId="195"> 
    <bookName>195</bookName>  
    <price>195.0</price> 
  </book>  
  <book bookId="200"> 
    <bookName>200</bookName>  
    <price>200.0</price> 
  </book> 
</books>


public boolean add(Book book) {
		boolean flag = false;	//设置一个布尔类型作为判断数据是否添加成功
		try {
			Document doc = doc("./src/com/bibn/Test/bean/book.xml");//调用其他方法,读取xml文件
			Element rootEle = doc.getRootElement();//获取根节点对象
			Element bookIdEle = rootEle.addElement("book");//获取book标签的节点对象
			Element bookNameEle = bookIdEle.addElement("bookName");
			Element priceEle = bookIdEle.addElement("price");
			bookIdEle.addAttribute("bookId", book.getBookId());//添加bookI及其值
			bookNameEle.setText(book.getBookName());//添加标签的值
			priceEle.setText(String.valueOf(book.getPrice()));
			writeXml(doc);//调用外方法将doc的内容添加到xml文件中
			flag = true;
		} catch (DocumentException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}


public boolean delete(String bookId) {
		try {
			Document doc = doc("./src/com/bibn/Test/bean/book.xml");

			Element root = doc.getRootElement();
			Iterator<Element> iterator = root.elementIterator();//迭代器
			while (iterator.hasNext()) {
				Element next = iterator.next();
				if (next.attributeValue("bookId").equals(bookId)) {//获取的编号和迭代的编号是否相等

					// 获取根元素
					Element rootEle = doc.getRootElement();

					// 获取所有直接子元素,并移除
					rootEle.remove(next);
					// 删除id属性
					Attribute attribute = next.attribute("bookId");
					next.remove(attribute);

					writeXml(doc);
					return true;
				}
			}
		} catch (Exception e) {

		}
		return false;
	}


public boolean modify(Book book) {
		try {
			Document doc = doc("./src/com/bibn/Test/bean/book.xml");

			Element root = doc.getRootElement();
			Iterator<Element> iterator = root.elementIterator();
			while (iterator.hasNext()) {
				Element next = iterator.next();

				if (next.attributeValue("bookId").equals(book.getBookId())) {

					// Element contatcElem2 =
					// doc.getRootElement().element("book");
					// 3.通过增加同名的属性的方法,修改属性值
					next.addAttribute("bookId", "" + book.getBookId());
					/*
					 * 修改文本: 1.得到标签 2.修改文本
					 */
					Element nameElement = next.element("bookName");
					Element priceElement = next.element("price");
					nameElement.setText(book.getBookName());
					priceElement.setText("" + book.getPrice());
					// 1.创建输出流通道
					writeXml(doc);
					return true;
				}
			}
		} catch (Exception e) {
		}

		return false;
	}


public List<Book> select() throws DocumentException {

		list = new ArrayList<Book>();
		Book book = null;
		SAXReader saxReader = new SAXReader();
		Document doc = doc("./src/com/bibn/Test/bean/book.xml");
		Element root = doc.getRootElement();
		Iterator<Element> iterator = root.elementIterator();
		while (iterator.hasNext()) {
			Element next = iterator.next();
			Element bookName = next.element("bookName");
			Element price = next.element("price");
			book = new Book(next.attributeValue("bookId"), bookName.getText(),
					Double.parseDouble(price.getText()));
			list.add(book);
		}
		return list;
	}

省却的方法


public Document doc(String s) throws Exception {
		SAXReader saxReader = new SAXReader();
		Document doc = saxReader.read(new File(s));
		return doc;
	}

	public void writeXml(Document document) throws IOException {
		// 输出的格式器
		OutputFormat outputFormat = OutputFormat.createPrettyPrint();
		outputFormat.setEncoding("gbk");

		// xmlWriter
		FileWriter fileWriter = new FileWriter(
				"./src/com/bibn/Test/bean/book.xml");
		XMLWriter xmlWriter = new XMLWriter(fileWriter, outputFormat);
		xmlWriter.write(document);
		xmlWriter.flush();
		xmlWriter.close();
	}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值