dom4j解析xml文件和字符串

解析xml字符串,如下

public static void main(String[] args) {
		//System.out.println("cccc");
		String fStr="<?xml version='1.0' encoding='UTF-8'?>" +
				"<ROOT test='test123' cod='cod123'><Name>AAA</Name><Number>BBB</Number>" +
				"<Recording>http://10.15.57.174/wav/2008/10/29/WG37100/ext37102/10.15.57.71!1~R!10292008_064002!37102!67256479!Ext!NA!1179371583!R.wav</Recording>" +
				"<Orders>有</Orders></ROOT>";
		try {
			SAXReader reader = new SAXReader();
			 Document doc; 
			 doc = DocumentHelper.parseText(fStr); 

			//Document doc = reader.read(ffile); //读取一个xml的文件
			Element root = doc.getRootElement();
			Attribute testCmd= root.attribute("test");
			System.out.println(testCmd.getName()+"-***--"+testCmd.getValue());						 
			Element eName = root.element("Name");
			System.out.println("节点内容*--"+eName.getTextTrim());		
		
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

 

 

解析xml文件,xml文件如下

<?xml version="1.0" encoding="UTF-8"?>
<students>
	<student age="25">
		<name>AAA</name>
		<collage leader='班长'>xxx学校</collage>
		<telephone>13728704450</telephone>
		<notes>男,1958年出生</notes>
	</student>
	<student age="21">
		<name>BBB</name>
		<collage leader='学生'>YYY学校</collage>
		<telephone>1372sadgasg4450</telephone>
		<notes>男,1968年出生,出生地长沙</notes>
	</student>
	<student age="29">
		<name>CCC</name>
		<collage leader='班主任'>北京大学</collage>
		<telephone>13828704450</telephone>
		<notes>女,漂亮</notes>
	</student>
</students>

 

解析的代码如下

 

public void iterateWholeXML(String filename, HashMap<String, String> hm) {
		SAXReader saxReader = new SAXReader();
		try {
			Document document = saxReader.read(new File(filename));
			Element root = document.getRootElement();
			// 用于记录学生编号的变量
			int num = -1;
			// 遍历根结点(students)的所有孩子节点(肯定是student节点)
			for (Iterator iter = root.elementIterator(); iter.hasNext();) {
				Element element = (Element) iter.next();
				num++;
				// 获取person节点的age属性的值
				Attribute ageAttr = element.attribute("age");
				if (ageAttr != null) {
					String age = ageAttr.getValue();
					if (age != null && !age.equals("")) {
						hm.put(element.getName() + "-" + ageAttr.getName()
								+ num, age);
					} else {
						hm.put(element.getName() + "-" + ageAttr.getName()
								+ num, "20");
					}
				} else {
					hm.put(element.getName() + "-age" + num, "20");
				}
				// 遍历student结点的所有孩子节点(即name,college,telphone,notes),并进行处理
				for (Iterator iterInner = element.elementIterator(); iterInner
						.hasNext();) {
					Element elementInner = (Element) iterInner.next();
					if (elementInner.getName().equals("collage")) {
						hm.put(elementInner.getName() + num, elementInner
								.getText());
						// 获取college节点的leader属性的值
						Attribute leaderAttr = elementInner.attribute("leader");
						if (leaderAttr != null) {
							String leader = leaderAttr.getValue();
							if (leader != null && !leader.equals("")) {
								hm.put(elementInner.getName() + "-"
										+ leaderAttr.getName() + num, leader);
							} else {
								hm.put(elementInner.getName() + "-"
										+ leaderAttr.getName() + num, "leader");
							}
						} else {
							hm.put(elementInner.getName() + "-leader" + num,
									"leader");
						}
					} else {
						hm.put(elementInner.getName() + num, elementInner
								.getText());
					}
				}
			}
		} catch (DocumentException e) {
			e.printStackTrace();
		}
	}

 

你可能感兴趣的:(xml,ext)