关闭

Dom4J解析XML

121人阅读 评论(0) 收藏 举报
分类:
public class ParseXMLByDOM4J implements ParseXML
{
	public Person getPersonByID(String path, String id)
	{
		Person person = new Person();

		// 获取到XML文件首层元素所有内容
		Element users = readXMLByDOM4J(path);

		for (Iterator<?> i = users.elementIterator(); i.hasNext();)
		{
			Element user = (Element) i.next();
			if (StringUtils.equals(user.attribute("id").getText(), id))
			{
				person = buildPerson(person, user);
			}
		}

		return person;
	}

	public List<Person> getPersons(String path)
	{
		List<Person> personList = new ArrayList<Person>();
		Person person;

		// 获取到XML文件首层元素所有内容
		Element users = readXMLByDOM4J(path);
		for (Iterator<?> i = users.elementIterator(); i.hasNext();)
		{
			person = new Person();
			Element user = (Element) i.next();
			personList.add(buildPerson(person, user));
		}
		return personList;
	}

	private Element readXMLByDOM4J(String path)
	{
		File inputXml = new File(path);
		SAXReader saxReader = new SAXReader();

		Element result = null;
		try
		{
			Document document = (Document) saxReader.read(inputXml);
			result = document.getRootElement();
		}
		catch (DocumentException e)
		{
			e.printStackTrace();
		}
		return result;
	}

	private Person buildPerson(Person person, Element user)
	{
		for (Iterator<?> j = user.elementIterator(); j.hasNext();)
		{
			Element node = (Element) j.next();

			if (StringUtils.equals(node.getName(), "name"))
			{
				person.setName(node.getText());
			}

			if (StringUtils.equals(node.getName(), "age"))
			{
				person.setAge(Integer.parseInt(node.getText()));
			}

			if (StringUtils.equals(node.getName(), "hobbit"))
			{
				person.setHobbit(node.getText());
			}
		}
		return person;
	}
}


实际处理类

public class PersonServiceImpl implements PersonService
{
	private static final String XML_PATH = "src/main/resource/person.xml";
	private static ParseXML parser = new ParseXMLByDOM4J();

	public List<Person> getAllPerson()
	{
		return parser.getPersons(XML_PATH);
	}

	public void addPerson(Person person)
	{
		File inputXml = new File(XML_PATH);
		SAXReader saxReader = new SAXReader();

		try
		{
			Document document = (Document) saxReader.read(inputXml);
			Element rootElement = document.getRootElement();
			Element personEle = rootElement.addElement("person");
			personEle.addAttribute("id", String.valueOf(person.getId()));

			Element name = personEle.addElement("name");
			name.setText(person.getName());

			Element age = personEle.addElement("age");
			age.setText(String.valueOf(person.getAge()));

			Element hobbit = personEle.addElement("hobbit");
			hobbit.setText(person.getHobbit());

			saveXML(document);
		}
		catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void deletePersonByID(String id)
	{
		File inputXml = new File(XML_PATH);
		SAXReader saxReader = new SAXReader();

		try
		{
			Document document = (Document) saxReader.read(inputXml);
			Element rootElement = document.getRootElement();

			for (Iterator<?> i = rootElement.elementIterator(); i.hasNext();)
			{
				Element user = (Element) i.next();
				if (StringUtils.equals(user.attribute("id").getText(), id))
				{
					rootElement.remove(user);
					break;// 找到指定的id处理完毕后就跳出,节省性能
				}
			}
			saveXML(document);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:65239次
    • 积分:1094
    • 等级:
    • 排名:千里之外
    • 原创:46篇
    • 转载:2篇
    • 译文:0篇
    • 评论:4条
    文章分类
    最新评论