`
xu283900277xiu
  • 浏览: 39138 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
文章分类
社区版块
存档分类

jdom解析、生成xml

阅读更多
接上篇,今天说说用JDOM解析、生成XML文件的简单小例子。
1、解析
XML文件内容如下:
<?xml version="1.0" encoding="utf-8"?>
<root>
	<request type="Pending" flowType="GENERAL" flowName="报销流程"
		docId="185647" flowId="16409" nodeName="报销人确认" wikId="58288"
		sendId="1210040" userId="1210040" createDate="2009-12-03"
		title="费用报销 " flowCreaterId="1210040" nodeType="1"
		bosTime="2009-12-03 09:36:15">
		<pro type="att"></pro>
		<pro type="textarea" name="OP_bxryj" title="处理意见" need="true"></pro>
	</request>
</root>

JAVA代码:
public void parseXML(String xmlPath){
		/* //创建一个新的字符串
        StringReader read = new StringReader(xml);
        //创建新的输入源SAX 解析器将使用 InputSource 对象来确定如何读取 XML 输入
        InputSource source = new InputSource(read);
        //创建一个新的SAXBuilder
        SAXBuilder sb = new SAXBuilder();*/
        
		   SAXBuilder sb = new SAXBuilder();
			
        try {
           //创建文档
        	Document doc = sb.build(new FileInputStream(xmlPath));
			Element root = doc.getRootElement();//获得XML的根元素
			System.out.println(root.getName());
			System.out.println("------------------------------");
			List list = root.getChildren();
			for(int i = 0; i < list.size(); i++){
				Element req = (Element)list.get(i);
				System.out.println(req.getName());
				System.out.println(req.getAttributeValue("type"));
				System.out.println(req.getAttributeValue("flowName"));
				System.out.println();
				List att_list = req.getAttributes();
				for(int j = 0; j < att_list.size(); j++){
					Attribute att = (Attribute)att_list.get(j);
					System.out.println(att.getName());
					System.out.println(att.getValue());
					System.out.println(att.getQualifiedName());
				}
				
				System.out.println();
				Attribute att = req.getAttribute("nodeName");
				System.out.println(att.getName());
				System.out.println(att.getValue());
			}
			System.out.println("--------------------------------------------");
			List pro_list = XPath.selectNodes(root, "request/pro");
			for(int k = 0; k < pro_list.size(); k++){
				Element ele = (Element)pro_list.get(k);
				System.out.println(ele.getName());
				System.out.println(ele.getText());
				System.out.println(ele.getTextTrim());
			}
			System.out.println("-------------------------------------------");
			
			Element pro = (Element)XPath.selectSingleNode(doc, "root//pro[@type='att']");
			System.out.println(pro.getName());
			System.out.println(pro.getValue());
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
	}

--------------------------------------------------------------------
2、生成XML文件
代码如下
public void createXML(){
		Element root = new Element("root");//创建根元素
		Document doc=new Document(root); 
		Element req = new Element("request");//根元素的子元素
		root.addContent(req);//添加到根元素中
		req.setAttribute("type", "test");//创建属性元素
		req.setAttribute("name","测试");
		
		Element pro = new Element("pro");//创建PRO元素
		Attribute att = new Attribute("pro_type","pro_test");//用不到的方法创建属性元素
		pro.setAttribute(att);
		pro.setText("设置TEXT");
		req.addContent(pro);
		
		Element cd = new Element("pro");
		cd.setAttribute("cd_type","cd_test");
		CDATA cdata = new CDATA("哈哈我找到了");//设置CDATA元素
		cd.addContent(cdata);
		req.addContent(cd);
		
		//输出结果到编辑器
		XMLOutputter out = new XMLOutputter();  
		out.setFormat(Format.getCompactFormat().setEncoding("GB2312"));
		System.out.println(out.outputString(doc));
	}
1
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics