dom4j Execute XML Demo_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > dom4j Execute XML Demo

dom4j Execute XML Demo

 2011/10/28 8:18:16  gengzg  http://gengzg.iteye.com  我要评论(0)
  • 摘要://ExcuteCreateXMLJOptionPane.showMessageDialog(null,"SDJFJLKDSF");System.out.println("SDDDDDDDDDDDDDDDD");Documentdocument=DocumentHelper.createDocument();Elementele=document.addElement("Root");ele.addComment("JSDFJKLLJKSDFJKLLJKSDFJKLKJFLDKSJLSFJ")
  • 标签:XML
		//Excute Create XML
		JOptionPane.showMessageDialog(null, "SDJFJLKDSF");
		System.out.println("SDDDDDDDDDDDDDDDD");
		Document document = DocumentHelper.createDocument();
		Element ele = document.addElement("Root");
		ele.addComment("JSDFJKLLJKSDFJKLLJKSDFJKLKJFLDKSJLSFJ");
		for (Integer i = 0; i < 50; i++) {
			Element e = ele.addElement("UserInfo");
			e.addElement("UserID").setText(i.toString());
			e.addElement("UserName").setText("耿宗国");
			e.addElement("UserEmail").setText("gengzg@126.com");
		}
		try {
			OutputFormat format = new OutputFormat();
			format.setEncoding("gb2312");
			format.setNewlines(true);
			XMLWriter write = new XMLWriter(new FileWriter("c://a.xml"), format);
			write.write(document);
			write.close();
			System.out.println("close");
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

?

		//Excute Reader XML
		SAXReader reader = new SAXReader();
		try {
			Document doc = reader.read("C://a.xml");
			Element root=doc.getRootElement();
			XmlVisitor visitor=new XmlVisitor();
			root.accept(visitor);
			visitor.visit(root);
			OutputFormat F=OutputFormat.createPrettyPrint();
			
			F.setEncoding("GB2312");
			F.setNewlines(true);
			try {
				XMLWriter w=new XMLWriter(new FileWriter("C://a.xml"),F);
				w.write(doc);
				w.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//visitor.visit(root);

		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

?

//Visitor XML
package Form;

import org.dom4j.Attribute;
import org.dom4j.Element;
import org.dom4j.VisitorSupport;

public class XmlVisitor extends VisitorSupport {

	@Override
	public void visit(Element node) {
		// TODO Auto-generated method stub
		super.visit(node);
		if(node.getName().equals("UserID")&&node.getText().equals("1"))
		{
			System.out.println(node.getName()+"----"+ node.getText());
			node.setName("ExUserID");
			node.setText("1111111");
			node.addAttribute("ID","0000");
		}
		if(node.attributeCount()>0)
		{
			for(Integer i=0;i<node.attributeCount();i++)
			{
				System.out.println(node.attribute(i).getName());
				if(node.attribute(i).getName().equals("ID"))
				{
					node.addAttribute("ExExUserID",node.attribute("ID").getText());
					node.remove(node.attribute(i));
//					node.attribute(i).setName("ExExUserID");
				}
			}
		}
	}

	@Override
	public void visit(Attribute node) {
		// TODO Auto-generated method stub
//		super.visit(node);
		System.out.println(node.getName());
	}

}

?

发表评论
用户名: 匿名