[英]Parse xml data using sax parser in java
我想解析如下的xml數據
xml數據
<?xml version="1.0" encoding="utf-8"?>
<markers>
<setMarker id="56c6b9c4ef263869678b4567" mlat="12.947014112057" mlng="77.62656211853"
slot_id="morning" />
<setMarker id="56c6b9c5ef263869678b4568" mlat="12.941744189945" mlng="77.628879547119"
slot_id="morning" />
<setMarker id="56c6ba01ef263805688b4567" mlat="12.929865544388" mlng="77.633428573608"
slot_id="morning" />
<setMarker id="56c6ba11ef2638c3668b4569" mlat="12.93630685197" mlng="77.613000869751"
slot_id="morning" />
<setMarker id="56c6ba1aef263814688b4567" mlat="12.944755587651" mlng="77.617979049683"
slot_id="morning" />
<setMarker id="56c6ba2bef26383e678b4567" mlat="12.928359760197" mlng="77.609052658081"
slot_id="morning" />
<setMarker id="56c6ba39ef263804678b4568" mlat="12.917317065284" mlng="77.60853767395"
slot_id="morning" />
<setMarker id="56c6ba3eef263849678b4567" mlat="12.908783739405" mlng="77.609825134277"
slot_id="morning" />
<setMarker id="56c6ba4eef263852678b4567" mlat="12.917735358079" mlng="77.586135864258"
slot_id="morning" />
<setMarker id="56c6ba58ef26383e678b4568" mlat="12.929196308091" mlng="77.583990097046"
slot_id="morning" />
</marker>
但是當我解析它為每個字段提供空值。 盡管在給出這種xml格式時解析器可以正常工作-
工作格式
<?xml version="1.0" encoding="UTF-8"?>
<markers>
<setMarker>
<mlat>"2163</mlat>
<mlng>Kumar</mlng>
<slot_id>Development</slot_id>
</setMarker>
<setMarker>
<mlat>6752</mlat>
<mlng>Siva</mlng>
<slot_id>DB</slot_id>
</setMarker>
<setMarker>
<mlat>6763</mlat>
<mlng>Timmy</mlng>
<slot_id>Testing</slot_id>
</setMarker>
</markers>
如何使用sax解析器解析該格式的xml數據?
您說不會產生預期結果的XML格式具有作為屬性的值。 嘗試使用Attribute類方法-getValue()讀取值。
像這樣訪問元素的屬性。
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
if(localName.equalsIgnoreCase("setMarker")){
String id = attributes.getValue("id");
String mlat = attributes.getValue("mlat");
String mlng = attributes.getValue("mlng");
String slot_id = attributes.getValue("slot_id");
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.