dom4j读取 XML_1(google天气为例)

本来是想用dom4j直接读取url的,但是读取url时,会出现

org.dom4j.DocumentException: Error on line 1 of document  : Content is not allowed in prolog. Nested exception: Content is not allowed in prolog. 的错误。

google后,说是多了个空格,哎,url多了个空格咋搞?

请教高手指点

 

有的xml中含有中文,这里拿google的天气api来做说明

 

如何让dom4j读取xml的中文呢(当然需要2个必备包,附件下载即可)

 

private Document readXML(String url) throws MalformedURLException,
			DocumentException, UnsupportedEncodingException {
		SAXReader reader = new SAXReader();
		byte[] bytes = url.getBytes();
		InputStream in = new ByteArrayInputStream(bytes);
		InputStreamReader strInStream = new InputStreamReader(in, "GBK"); 
		Document document = reader.read(strInStream);
		return document;
	}

 这样就可以读取中文了

Document weatherDoc = null;
		try {
			weatherDoc = readXML("http://www.google.com/ig/api?hl=zh-cn&weather=changzhou");
			//weatherDoc = readXML(new File("c://api.xml"));
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		//List list = weatherDoc.selectNodes("//xml_api_reply/weather/forecast_information");
        Node node = weatherDoc.selectSingleNode("//xml_api_reply/weather/current_conditions/condition");
        System.out.println(node.getName());
        String name = node.valueOf("@data");
        System.out.println(name);

 @data出来了,今天“

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值