2012-06-14 99 views
0

我從一個web服務在一個變量XML輸出的「結果」解析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獲得緯度和經度,並繪製它的谷歌地圖..

任何想法我怎麼能做到這一點.. 請幫助

感謝名單提前.. :))

+0

你嘗試過什麼到目前爲止的例子?有2個不同的步驟;解析,然後繪圖。恕我直言,把這兩個在同一個問題將減少你得到的答案的數量/質量,因爲你要求很多代碼。 – Basic

回答

2

我最喜歡解析xml的方法是使用JAXB。您需要一個XML模式定義文件,以說明如何構建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> 
+0

這工作非常好.. thanx很多 –