0

我在变量“结果”中有一个来自 Web 服务的 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 以获取纬度和经度并将其绘制在谷歌地图上..

知道我该怎么做..请帮忙

提前谢谢.. :))

4

2 回答 2

2

我最喜欢的解析 xml 的方法是使用 JAXB。您需要一个 XML Schema Definition 文件来说明 XML 的结构。然后使用 JAXB 生成表示元素的 Java 类。然后您可以将 XML/数据编组和解组进出这些对象。

//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>
于 2012-06-14T18:12:11.667 回答
0

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

于 2012-06-14T18:10:41.023 回答