用dom4j解析xml文件

将字符串转换成xml文件,在读取出xml中的节点。

1.java类:

            Document doc = DocumentHelper.parseText(decStr);
			 // 拿到根节点
			Element root = doc.getRootElement();
			// 拿到根节点的名称
			System.out.println("根节点:" + root.getName());
			// 获取根节点下的子节点head
			Iterator iter = root.elementIterator("PlainMsg"); 
			while (iter.hasNext()) { // 遍历head节点
				Element recordEle = (Element) iter.next();
				Element eletxt = recordEle.element("uploadtxtFile");
				Element eleflag = recordEle.element("uploadflagFile");
				// 拿到head节点下的子节点uploadtxtFile值
				txtfile = recordEle.elementTextTrim("uploadtxtFile"); 
				flagfile = recordEle.elementTextTrim("uploadflagFile");
				flagfilede = new String(pki.Base64Decode(flagfile));
				txtname = eletxt.attributeValue("name");
				flagName = eleflag.attributeValue("name");
				
			}

2.xml文件

<MsgPart>
<PlainMsg>
<secureMsg></secureMsg>
<clientCert></clientCert>
<y></y>
<q></q>
<sjqmhzjm>qsscwttj</sjqmhzjm>
<entype></entype>
<gid>127</gid>
<uploadtxtFile  type='file' name='测试文档一.txt'>txt格式测试文档</uploadtxtFile>
<uploadflagFile  type='file' name='测试文档一.flag'>flag格式测试文档</uploadflagFile>
</PlainMsg>
<Signature>
</Signature>
</MsgPart>

 

 

 

 

 

 

你可能感兴趣的:(用dom4j解析xml文件)