用xstream解析谷歌地理编码 [英] parse google geocode with xstream

查看:102
本文介绍了用xstream解析谷歌地理编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Java和XStream通过http解析谷歌地理编码请求。我的想法是拥有一个带有所有地理编码attr的地址类(即lat / long,city,provice / state等)但我在使用xstream解析xml时遇到问题。

I'm using Java and XStream to parse a google geocode request over http. My idea is to have an Address class with all the geocode attr's (ie. lat/long, city, provice/state etc) but I'm having problems parsing the xml with xstream.

谷歌的回复类似于:

<?xml version="1.0" encoding="UTF-8" ?>
<kml xmlns="http://earth.google.com/kml/2.0"><Response>
  <name>98 St. Patrick St, Toronto</name>
  <Status>
    <code>200</code>
    <request>geocode</request>
  </Status>
  <Placemark id="p1">
    <address>98 St Patrick St, Toronto, ON, Canada</address>
    <AddressDetails Accuracy="8" xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0"> <Country><CountryNameCode>CA</CountryNameCode><CountryName>Canada</CountryName><AdministrativeArea><AdministrativeAreaName>ON</AdministrativeAreaName><Locality><LocalityName>Toronto</LocalityName><Thoroughfare><ThoroughfareName>98 St Patrick St</ThoroughfareName></Thoroughfare><PostalCode><PostalCodeNumber>M5T</PostalCodeNumber></PostalCode></Locality></AdministrativeArea></Country></AddressDetails>
    <ExtendedData>
      <LatLonBox north="43.6560378" south="43.6497426" east="-79.3864912" west="-79.3927864" />
    </ExtendedData>
    <Point><coordinates>-79.3896388,43.6528902,0</coordinates></Point>
  </Placemark>
</Response></kml>

这并没有很好地显示,但代码的内容在AddressDetails标签中。

That doesn't show up very well, but the meat of the code is in the AddressDetails tag.

无论如何,我是Java和XStream的新手,所以API术语对我来说有点混乱。我只需要能够编写一些映射器,将所有这些标记(即CountryName)映射到我的Address对象中的属性,(即。address.country = blah)地址对象将非常简单,主要是国家的字符串名称等,并为lat / long浮动。

Anyway, I'm new to Java and XStream so the API terminology is a bit confusing for me. I just need to be able to write some mapper that maps all these tags (ie. CountryName) to an attribute within my Address object, (ie. address.country = blah) The address object will be pretty simple, mainly just strings for country name etc and floats for lat/long.

文档和示例只显示直接映射,其中每个xml标记直接映射到对象的同名属性。然而,在我的情况下,标签的名称与对象attr的不同。我正在寻找正确方向的快速点。

The docs and example just show straight mapping where each xml tag maps directly to the attribute of the same name of the object. In my case however, the tags are named different than the object attr's. A quick point in the right direction is all I'm looking for really.

推荐答案

我在几个项目中使用了XStream。不幸的是,你的问题并不是XStream旨在解决的问题。您可以使用其转换器机制来实现您的直接目标,但您将遇到限制。简而言之,XStream不是为了将树结构A转换为树结构B而设计的 - 它的目的是将Java域模型转换为一些合理的XML。当您不太关心所生成的XML的细节时,XStream是一个很好的工具。如果您更关心XML而不是Java对象,请查看XMLBeans - Java很难看,但它非常符合模式。

I've used XStream in several projects. Unfortunately, your problem isn't really what XStream is designed to solve. You might be able to use its converter mechanism to achieve your immediate goal, but you'll run into limitations. In a nutshell, XStream isn't designed to do conversion of Tree Structure A into Tree Structure B -- it's purpose is to convert from a Java domain model into some reasonable XML. XStream is a great tool when you don't care much about the details of the XML produced. If you care more about the XML than the Java objects, look at XMLBeans -- the Java is ugly, but it's incredibly schema-compliant.

对于您的项目,我' d通过XML bean运行Google XML模式,生成一些Java,它将为您提供一种更有文化的手动编码转换器的方法。您可以使用原始DOM树,但是您的代码类似于myAddress.setStreet(root.getFirstChild()。getAttribute(addr1)))。使用XML bean,你会说像myAddress.setStreet(googleResult.getAddress()。getStreetName();

For your project, I'd run the Google XML schema through XML beans, generate some Java that will give you a more literate way of hand-coding a converter. You could use a raw DOM tree, but you'd have code like myAddress.setStreet(root.getFirstChild().getAttribute("addr1"))). With XML beans, you say things like myAddress.setStreet(googleResult.getAddress().getStreetName();

我会忽略JAXB,因为它试图将接口与实现分开增加了不必要的复杂性.Castor可能也是一个很好的考虑工具,但我多年没有使用过它。

I'd ignore JAXB as it's attempt to separate interface from implementation adds needless complexity. Castor might be a good tool to consider as well, but I haven't used it in years.

简而言之,不是很多良好的Object-to-Object或XML-to-to转换器可以很好地处理结构转换。在那些我见过的尝试声明性解决方案中,所有这些都比使用XStream / XmlBeans更复杂(并且不再可维护)手动编码结构转换。

In a nutshell, there aren't a lot of good Object-to-Object or XML-to-Object converters that handle structure conversion well. Of those I've seen that attempt declarative solutions, all of them seemed much more complicated (and no more maintainable) than using XStream/XmlBeans along with hand-coded structure conversions.

这篇关于用xstream解析谷歌地理编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆