xml 操作
下面是对xml实现CRUD的实例
JaxpXMLUtil.java
package com.braden.util;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class JaxpXMLUtil {
String path = "";
public JaxpXMLUtil(String path) {
this.path = path;
}
public Document getDocument() {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
Document document = null;
try {
builder = factory.newDocumentBuilder();
document = builder.parse(path);
} catch (Exception e) {
throw new RuntimeException(e);
}
return document;
}
public boolean writeToXml(Document document, String path) {
boolean flag = false;
TransformerFactory tFactory = TransformerFactory.newInstance();
try {
Transformer tf = tFactory.newTransformer();
if (path != null && path != "") {
tf.transform(new DOMSource(document), new StreamResult(
new File(path)));
flag = true;
} else {
flag = false;
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return flag;
}
public String getId() {
Document document = this.getDocument();
NodeList list = document.getDocumentElement().getChildNodes();
int id = 1;
if (list != null) {
int len = list.getLength();
for (int i = 0; i < len; i++) {
Node node = list.item(i);
if (node.getNodeType()==Node.ELEMENT_NODE) {
id++;
}
}
}
return id+"";
}
}
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class JaxpXMLUtil {
String path = "";
public JaxpXMLUtil(String path) {
this.path = path;
}
public Document getDocument() {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
Document document = null;
try {
builder = factory.newDocumentBuilder();
document = builder.parse(path);
} catch (Exception e) {
throw new RuntimeException(e);
}
return document;
}
public boolean writeToXml(Document document, String path) {
boolean flag = false;
TransformerFactory tFactory = TransformerFactory.newInstance();
try {
Transformer tf = tFactory.newTransformer();
if (path != null && path != "") {
tf.transform(new DOMSource(document), new StreamResult(
new File(path)));
flag = true;
} else {
flag = false;
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return flag;
}
public String getId() {
Document document = this.getDocument();
NodeList list = document.getDocumentElement().getChildNodes();
int id = 1;
if (list != null) {
int len = list.getLength();
for (int i = 0; i < len; i++) {
Node node = list.item(i);
if (node.getNodeType()==Node.ELEMENT_NODE) {
id++;
}
}
}
return id+"";
}
}
UserDaoImpl.java
package com.braden.impl; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import com.braden.dao.UserDao; import com.braden.domain.User; import com.braden.util.JaxpXMLUtil; public class UserDaoImpl implements UserDao { JaxpXMLUtil util = null; public UserDaoImpl() { util = new JaxpXMLUtil("src/user.xml"); } public boolean delete(String id) { boolean flag = false; Document document = util.getDocument(); NodeList user_list = document.getElementsByTagName("user"); for (int i = 0; i < user_list.getLength(); i++) { Element e = (Element) user_list.item(i); if (e.getAttribute("id").equals(id)) { Node node = user_list.item(i); e.getParentNode().removeChild(node); util.writeToXml(document, "src/user.xml"); flag = true; } } return flag; } public User find(String id) { Document document = util.getDocument(); NodeList user_list = document.getElementsByTagName("user"); User user = null; for (int i = 0; i < user_list.getLength(); i++) { Element e = (Element) user_list.item(i); if (e.getAttribute("id").equals(id)) { user = new User(); Node addressNode = e.getElementsByTagName("address").item(0); Node bitthdayNode = e.getElementsByTagName("bitthday").item(0); Node genderNode = e.getElementsByTagName("gender").item(0); Node usernameNode = e.getElementsByTagName("username").item(0); Node passwordNode = e.getElementsByTagName("password").item(0); user.setAddress(addressNode.getTextContent()); user.setBitthday(bitthdayNode.getTextContent()); user.setGender(genderNode.getTextContent()); user.setUsername(usernameNode.getTextContent()); user.setPassword(passwordNode.getTextContent()); break ; } } return user; } public boolean update(String id, String updateitem, String updateinfo) { boolean flag = false; Document document = util.getDocument(); NodeList user_list = document.getElementsByTagName("user"); for (int i = 0; i < user_list.getLength(); i++) { Element e = (Element) user_list.item(i); if (e.getAttribute("id").equals(id)) { NodeList user_child_List = e.getChildNodes(); for (int j = 0; j < user_child_List.getLength(); j++) { Node node_item = user_child_List.item(j); if (node_item.getNodeName().equals(updateitem)) { if (!node_item.getTextContent().equals(updateinfo)) { node_item.setTextContent(updateinfo); util.writeToXml(document, "src/user.xml"); flag = true; } } } } } return flag; } public boolean insert(User user) { Document document = util.getDocument(); /* System.out.println(document.getDocumentURI()); */ Element node_user = document.createElement("user"); node_user.setAttribute("id", user.getId()); Element node_username = document.createElement("username"); node_username.setTextContent(user.getUsername()); Element node_id = document.createElement("id"); node_id.setTextContent(user.getId()); Element node_gender = document.createElement("gender"); node_gender.setTextContent(user.getGender()); Element node_password = document.createElement("password"); node_password.setTextContent(user.getPassword()); Element node_bitthday = document.createElement("bitthday"); node_bitthday.setTextContent(user.getBitthday()); Element node_address = document.createElement("address"); node_address.setTextContent(user.getAddress()); node_user.appendChild(node_username); node_user.appendChild(node_bitthday); node_user.appendChild(node_address); node_user.appendChild(node_gender); node_user.appendChild(node_password); document.getDocumentElement().appendChild(node_user); try { util.writeToXml(document, "src/user.xml"); return true; } catch (Exception e) { throw new RuntimeException(e); } } }
UserDaoImplTest.java
package com.braden.junit;
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.xml.sax.SAXException;
import com.braden.dao.UserDao;
import com.braden.domain.User;
import com.braden.impl.UserDaoImpl;
import com.braden.util.JaxpXMLUtil;
public class UserDaoImplTest {
static UserDao dao;
static JaxpXMLUtil util;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
dao = new UserDaoImpl();
util = new JaxpXMLUtil("src/user.xml");
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
}
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public void testDelete() {
if (dao.delete("2")) {
System.out.println("删除成功");
} else {
System.out.println("删除失败");
}
}
@Test
public void testFind() {
User user = dao.find("2");
if (user!=null) {
System.out.println(user.getPassword());
} else {
System.out.println("用户不存在");
}
}
@Test
public void testUpdate() {
if(dao.update("2", "password","lina")) {
System.out.println("更新成功");
} else {
System.out.println("更新失败");
}
}
@Test
public void testInsert() throws SAXException, IOException, ParserConfigurationException {
String id =util.getId();
User user = new User();
user.setAddress("beijing"+id+"beijing");
user.setBitthday("1984-09-03");
user.setId(id);
user.setUsername("braden_"+id);
user.setGender("female");
user.setPassword("123456");
try {
dao.insert(user);
System.out.println("user" + user.getId()+"添加成功!");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
User.java
package com.braden.domain;
public class User {
private String username;
private String id;
private String bitthday;
private String address;
private String gender;
private String password;
public User() {
super();
}
public User(String username, String id, String bitthday, String address,
String gender, String password) {
super();
this.username = username;
this.id = id;
this.bitthday = bitthday;
this.address = address;
this.gender = gender;
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getBitthday() {
return bitthday;
}
public void setBitthday(String bitthday) {
this.bitthday = bitthday;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
得到的xml文件
<?xml version="1.0" encoding="UTF-8" standalone="no"?><!-- <!DOCTYPE user PUBLIC "//UNKNOWN/" "user.dtd"> --> <users> <user id="1"> <username>braden_1</username> <bitthday>1984-09-03</bitthday> <address>beijing1beijing</address> <gender>female</gender> <password>123456</password> </user> <user id="2"> <username>braden_2</username> <bitthday>1984-09-03</bitthday> <address>beijing2beijing</address> <gender>female</gender> <password>123456</password> </user> <user id="3"> <username>braden_3</username> <bitthday>1984-09-03</bitthday> <address>beijing3beijing</address> <gender>female</gender> <password>123456</password> </user> </users>
相关推荐
这里提到的"xml操作xml操作xml操作xml操作xml操作"显然是指对XML文档进行读取、修改和创建的操作。 在给定的示例中,首先加载XML文档到`XmlDocument`对象,这是.NET框架中用于处理XML的标准类。通过`XmlDocument`的...
总结来说,"基于tinyxml2的xml操作库再封装"项目为C++开发者提供了一个高效、易用的XML处理工具,通过优化和扩展tinyxml2的功能,使得XML数据的处理变得更加直观和便捷。这将极大地提高开发效率,降低开发成本,尤其...
**基于QT封装的XML操作与显示类** 在软件开发中,XML(eXtensible Markup Language)作为一种数据交换和存储格式,广泛应用于不同系统间的数据交互。QT作为一个跨平台的C++图形用户界面应用程序框架,提供了对XML的...
本篇文章将深入探讨如何在C#中进行XML操作,主要基于提供的"XML操作类"的压缩包文件。 首先,XML操作类通常包含了读取、写入、解析和修改XML文档的一系列方法。在C#中,我们可以利用System.Xml命名空间下的类来实现...
以下是一些关于XML操作的关键知识点: 1. **XML基本结构**: - XML文档由元素(Element)组成,元素是XML的最基本单位,可以包含其他元素或文本。 - 元素以尖括号 `和 `>` 包裹,如 `<element>`,并用 `/` 表示...
本项目案例“java对xml操作的项目案例”深入探讨了如何使用Java来读取、修改和写入XML文件。下面将详细阐述相关知识点。 一、Java与XML的交互 1. DOM解析器:Document Object Model (DOM) 是一种将整个XML文档加载...
动态生成Rss文件 Xml操作 Xml文件的修改动态生成Rss文件 Xml操作 Xml文件的修改动态生成Rss文件 Xml操作 Xml文件的修改动态生成Rss文件 Xml操作 Xml文件的修改动态生成Rss文件 Xml操作 Xml文件的修改动态生成Rss文件...
在提供的源码文件“Asp[1].net+操作XML+源码.rar”中,可能包含了具体的ASP.NET XML操作示例,包括如何创建XML文件、读取XML文件、更新XML节点以及删除XML节点的代码片段。而“WebSite2”可能是包含一个完整的Web...
tinyxml,很好用的xml操作类,c++版,删除了乱七八糟的工程文件,用时include一下两个头文件即可。具体使用方法参考:http://blog.csdn.net/zhangrentian/archive/2010/01/15/5194875.aspx
XML操作类(完整)使用于C#领域,更好的操作XML文件
c#下操作xml文件类,方便调用操作。 xml操作日趋常见,xml格式相对规范,通过xml存储信息更加方法,采用该类可以方便操作。
最近做项目,要用C#写,而且数据格式是那种节点套节点能把人套死的XML,看着都觉得晕,而且节点跳跃性很大,真心觉得单纯用环境自带的XMLDocument类完全达不到自己的需要,因为XML里面涉及到节点循环,循环里面又套...
Xml操作类,项目中使用的,包含读取数据、插入数据、修改数据、删除数据 附带使用方法。
xml教程(增删改)xml教程 xml xml操作教程 xml读取-程序员百味www.bywei.cnxml教程(增删改)xml教程 xml xml操作教程 xml读取-程序员百味www.bywei.cnxml教程(增删改)xml教程 xml xml操作教程 xml读取-程序员百...
xml的所有操作.xml的所有操作.xml的所有操作.xml的所有操作.
XML操作类XML操作类XML操作类XML操作类XML操作类XML操作类XML操作类XML操作类
该代码主要实现了根据xml中定义的扩展字段Expand动态生成textbox,填写后更新xml的一个源码,包括xml的增删改查功能。算是比较全的一个xml操作源码了。主要是为了公司项目做的可行性研究,欢迎交流分享。
asp.net 用xml操作wordasp.net 用xml操作wordasp.net 用xml操作wordasp.net 用xml操作wordasp.net 用xml操作wordasp.net 用xml操作wordasp.net 用xml操作wordasp.net 用xml操作wordasp.net 用xml操作wordasp.net 用...
2. **C#中的XML操作库**:在C#中,我们主要使用System.Xml命名空间中的类来处理XML。主要包括XmlDocument、XmlNode、XmlElement等。XmlDocument是整个XML文档的容器,XmlNode是所有XML节点的基类,XmlElement表示...
以上就是关于XML操作的基本介绍,通过C#的`XmlDocument`、`XmlWriter`、`XmlNode`以及`DataSet`等类,我们可以方便地实现XML的读取、写入、更新和删除。理解这些概念和用法,将有助于在实际开发中更好地处理和利用...