xml怎么读数据来?

dnpao 2012-11-26 03:00:31
<?xml version="1.0" encoding="UTF-8"?>
<urlset count="118"> // count="118" 北京地区有118个团购信息
<url id="1"> // id="1" 北京第1团购产品
<loc>http://beijing.lashou.com/deal/114728.html</loc>
<data>
<display>
<website>拉手网</website>
<cate>休闲</cate>
<siteurl>http://beijing.lashou.com/</siteurl>
<city>北京</city>
<gid>114728</gid>
<title><![CDATA[ 仅售39元!原价70元“中国航空博物馆”周末体验套票一张:模拟射击体验一次(20发)+登歼-6战机体验(自带相机)+空中医院+飞行头盔+飞行服装!本套票仅限周六至周日使用!体验射击快感和翱翔天空的酣畅淋漓!]]></title>
<image>http://d1.lashouimg.com/lasho_pic/go/00/00/11/47/114728</image>
<startTime>1307635200</startTime>
<endTime>1307894400</endTime>
<value>70.00</value> // value 该商品市场价
<price>39.00</price> // price 该商品折扣价
<rebate>5.6</rebate> // rebate 该商品的折扣
<bought>79</bought> // bought 该商品的购买人数
<detail>
<![CDATA[ 扛起战枪,体验激情四射的枪林弹雨;坐上战机,给孩子们一个无与伦比的军事体验。]]></detail>
<shops>
<shop>
<name><![CDATA[ 中国航空博物馆]]></name>
<tel><![CDATA[ ]]></tel>
<addr><![CDATA[ 昌平区小汤山中国航空博物馆]]></addr>
</shop>
</shops>
</display>
</data>
</url>
</urlset>
...全文
243 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
cds27 2012-11-30
  • 打赏
  • 举报
回复
可以上这里看一下http://blog.csdn.net/cds27/,上面有几种主流语言的XML解析示例。
xingfuzhuzi1314 2012-11-30
  • 打赏
  • 举报
回复

public List readXML(String xml) throws JDOMException, IOException{
		List<Map<String,String>> list=new ArrayList<Map<String,String>>();
		Map<String,String> map=new HashMap<String,String>();
		Document document = null;  
		ByteArrayInputStream xmlStream = null;        
		try{              
			xmlStream=new ByteArrayInputStream(xml.getBytes("gb2312"));        
			}         
		catch(UnsupportedEncodingException uee){               
			uee.printStackTrace();          
		}   
		if(xmlStream!=null){
			SAXBuilder saxBuilder=new SAXBuilder();                  
			document=saxBuilder.build(xmlStream);
			Element root=document.getRootElement();
			System.out.println(root.getName());
			list=this.xmlToMap(root, list,map);
			
		}
		return list;
	}
	public List<Map<String,String>> xmlToMap(Element root,List<Map<String,String>> list,Map<String,String> map){
		List children=root.getChildren();
		Iterator it = children.iterator();
		
		while(it.hasNext()){
		     
			Element child = (Element) it.next();   
			if(map.containsKey("loc")){
				map=new HashMap<String,String>();
			}
			 String name = child.getName(); 
			 System.out.println(name+"***"+child.getText());
			 
			 // 如果有子节点,则递归调用    
			 if (child.getChildren().size() > 0) 
			 {   
				 xmlToMap(child, list,map);    
			 }else{
				 if(child.getText()!=null && !child.getText().equals("")){
					 map.put(name, child.getText());
				 }
				
			 }
				 list.add(map);
			 }
			
		}
		return list;
楼主试试吧,后面的得根据你的业务来定,而且你这还有attribule,这个我没加进来!
conan8126 2012-11-26
  • 打赏
  • 举报
回复
http://verne.diandian.com/post/2010-04-08/40025346015
dnpao 2012-11-26
  • 打赏
  • 举报
回复
对xml不懂,求 代码!
ImN1 2012-11-26
  • 打赏
  • 举报
回复
不同语言不同做法 汉语+英语的话就用嘴巴读出来就行 一般是先parse xml结构,用xpath或者dom提取

8,906

社区成员

发帖
与我相关
我的任务
社区描述
XML/XSL相关问题讨论专区
社区管理员
  • XML/XSL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧