`

java 读xml

    博客分类:
  • java
阅读更多
应用环境webservice接口,通过xml传递数据。其实在使用公司接口时xml转换接口都被封装完成了。 下面是我测试本地代码是写的一点东东,以及遇到的一点问题。
我这里使用的是org.w3c.dom里面的接口读取xml的。
一、读取xml字符串
xmlPara= model.getXmlPara();
		//去不可见字符
		Pattern p = Pattern.compile("\\s*|\t|\r|\n");  
		Matcher m = p.matcher(xmlPara);  
		xmlPara = m.replaceAll("");  

		InputStream xmlIn =new ByteArrayInputStream(xmlPara.getBytes("utf-8"));
		InputSource xmlSource = new InputSource(xmlIn);
		DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder parser = docBuilderFactory.newDocumentBuilder();
		Document doc = parser.parse(xmlSource);
		Element root = doc.getDocumentElement();

第一点、在读取xml字符时因为有中文字符,必须根据实际编码来转换成输入流。第二点、必须去掉字符串中不可见字符,包括回车、空格、tab键等,这样在获取子节点是才不会出现莫名其妙的错误(比如:找的节点的名字不对,个数也不对)
二、xml字符串示例
<data>
	<orgNo>供电单位</orgNo>
	<countyCode>320101</countyCode>
	<certTypeCode>证件类型</certTypeCode>
	<certNo> 证件号码 </certNo>
	<custName>客户名称</custName>
	<elecAddr>用电地址</elecAddr>
	<contacts>
		<contact>
		<contactType>联系类型</contactType >
		<contactName>联系人</contactName>
		<mobile>手机号码</mobile>
		<addr>地址</addr>
		<tel>固定电话</tel>
		<email>电子邮箱</email>
		</contact>
	</contacts>
	<useArea>房屋使用面积</useArea>
	<postalCode>电费通知方式</postalCode>
	<consNo>用户编号</consNo>
	<resentPower>当前用电情况</resentPower>
	<legalAddr>法定地址</legalAddr>
	<legalPerson>法定代表人</legalPerson>
	<bankName></bankName >
	<bankNo>缴费银行账号</bankNo>
	<purchaseType>电费缴费方式</purchaseType >
	<appContent >申请说明</appContent>
</data>


对应的读取代码(  好记性不如烂笔头呵呵)

Map<String,Object> map = new HashMap<String,Object>();
		NodeList list = root.getChildNodes();
		Object nodeValue = null;
	    for (int i = 0; i < list.getLength(); ++i) {
			Node node = list.item(i);
			if (node != null) {
				String nodeName = node.getNodeName();
				if(nodeName.endsWith("s")){
					NodeList list1 = node.getChildNodes();
//					System.out.println(list1.);
					List<Map<String,String>> listIn = new ArrayList<Map<String,String>>();
					String node1Name = "";
					// 循环体,就是联系信息循环
					for(int j=0;j<list1.getLength();j++){
						//第一层循环
						Node node1 = list1.item(j);
						
						if(node1 != null ){
							node1Name = node1.getNodeName();
							NodeList list2 = node1.getChildNodes();
							System.out.println(list2.getLength());
							Map<String,String> map2= new HashMap<String,String>();
							for(int k=0;k<list2.getLength();k++){
								//第二次循环
								Node node3 = list2.item(k);
								if(node3 != null){
									Node child = node3.getFirstChild();
									String value = "";
									if ((child != null) && (child.getNodeValue() != null)) {
										value = child.getNodeValue();
							        }
									map2.put(node3.getNodeName(), value);
								}
							}// 第二次循环
							
							listIn.add(map2);
						}
					}//第一次循环
					map.put(nodeName+"#"+node1Name, listIn);
					continue;
				}
				Node child = node.getFirstChild();
				
				nodeValue = null;
		        if ((child != null) && (child.getNodeValue() != null)) {
		        	nodeValue = child.getNodeValue();
		        }
		        if(nodeValue == null){
		        	map.put(nodeName, "");
		        }else{
		        	map.put(nodeName, nodeValue);
		        }
			}
			
		}// end of 最外层循环
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics