我想解析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数据?
答案 0 :(得分:0)
您说的XML格式未产生预期结果,其值为属性。尝试使用Attribute类方法读取值 - getValue()。
答案 1 :(得分:0)
访问元素属性,如此。
@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");
}
}