解析xml(仅存储选定的数据)

时间:2012-06-14 18:03:31

标签: android eclipse google-maps xml-parsing

我在变量“results”

中有来自webservice的xml输出

它看起来像这样,

<ArrayOfSeriesRecord xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns="http://hiscentral.cuahsi.org/20100205/">

<SeriesRecord>
<ServCode>IIHRTippingB</ServCode>
<ServURL>
http://his08.iihr.uiowa.edu/tippingbucket/cuahsi_1_0.asmx?WSDL
</ServURL>
<location>IIHR:CC00</location>
<VarCode>IIHR:Precipitation</VarCode>
<VarName>Precipitation</VarName>
<beginDate>9/25/2006 12:00:00 AM</beginDate>
<endDate>5/7/2011 12:00:00 AM</endDate>
<ValueCount>161738</ValueCount>
<Sitename>CC00</Sitename>
<latitude>41.736152648925781</latitude>
<longitude>-91.931015014648438</longitude>
<datatype>Continuous</datatype>
<valuetype>FieldObservation</valuetype>
<samplemedium>Precipitation</samplemedium>
<timeunits>minute</timeunits>
<conceptKeyword>Precipitation</conceptKeyword>
<genCategory/>
<TimeSupport>15</TimeSupport>
</SeriesRecord>

我需要解析这个xml以获取纬度和经度并将其绘制在谷歌地图上..

任何想法我怎么能这样做.. 请帮忙

预先

thanx ..:))

2 个答案:

答案 0 :(得分:2)

我最喜欢的解析xml的方法是使用JAXB。您需要一个XML Schema Definition文件,该文件说明了XML的结构。然后使用JAXB生成表示元素的Java类。然后,您可以对这些对象进行Marshal和Unmarshal XML / Data。

//Read an XML file (or string, stream, etc...) into Java classes
JAXBContext context = JAXBContext.newInstance("org.my.jaxb.classes");
Unmarshaller unmarshaller = context.createUnmarshaller();

JAXBElement<ArrayOfSeriesRecord> rootJaxb = (JAXBElement<ArrayOfSeriesRecord>)unmarshaller.unmarshal(new File("file.xml"));
ArrayOfSeriesRecord root = rootJaxb.getValue();
List<SeriesRecord> seriesRecords = root.getSeriesRecord();

XSD示例

//Example xsd
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="ArrayOfSeriesRecord" type="ArrayOfSeriesRecordType"></xsd:element>

    <xsd:complexType name="ArrayOfSeriesRecordType">
        <xsd:sequence>
            <xsd:element name="SeriesRecord" type="SeriesRecordType" maxOccurs="unbounded"></xsd:element>
        </xsd:sequence>
    </xsd:complexType>

    <xsd:complexType name="SeriesRecordType">
        <xsd:sequence>
            <xsd:element name="ServCode" type="xsd:string"></xsd:element>
        </xsd:sequence>
    </xsd:complexType>
</xsd:schema>

答案 1 :(得分:0)

您可以使用xml解析器(如tinyXml:http://sourceforge.net/projects/tinyxml/)来解析文档,然后只需选择经度和纬度标记内部文本值。