複雑なXMLファイルからいくつかの属性を取得しています。
<rsp>
<csl d='10775.916760613756' id='2003' nam='AUTOS TEZIUTLÁN, S.A. DE C.V.'
adr='KM. 1 CARR. TEZIUTLÁN-TLAPACOYAN' tel='231312-12-05'
lat='19.826765' lon='-97.347906' />
<csl d='10789.680721293766' id='2019' nam='AUTOMOVILÍSTICA DE TEHUACAN, S.A. DE C.V.'
adr='BLVD. ADOLFO LOPEZ MATEOS NO. 3623' tel='238382-44-33'
lat='18.467281' lon='-97.417901' />
<csl d='10848.586325071066' id='2013' nam='AUTOMOTRIZ DE LA SIERRA, S.A. DE C.V.'
adr='AUSENCIO T. JIMÉNEZ No. 1' tel='776762-05-42'
lat='20.174386' lon='-98.06125' />
<csl d='10866.815936520663' id='2028' nam='MOTORES ALEMANES RIVERA S.A. DE C.V.'
adr='CALZADA IGNACIO ZARAGOZA NO. 180' tel='222286-02-02'
lat='19.064258' lon='-98.179042' />
<csl d='10867.374198658401' id='2012' nam='ARMENTA AUTOMOTRIZ, S.A. DE C.V.'
adr='24 NORTE No. 214' tel='222235-87-68'
lat='19.038912' lon='-98.183101' />
</rsp>
しかし、私のXQUERYは最初の属性と、このクラスを使用する属性のみを提供します。
public class XMLParser {
private String[] resultTable;
public XMLParser(){}
public String[] stringToXML(String xmlString) {
try{
DocumentBuilderFactory dBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dBuilderFactory.newDocumentBuilder();
InputSource iSource = new InputSource();
iSource.setCharacterStream(new StringReader(xmlString));
Document doc = dBuilder.parse(iSource);
NodeList nList = doc.getElementsByTagName("tpr");
resultTable = new String [nList.getLength()];
for (int i=0; i<nList.getLength(); i++){
Element e = (Element)nList.item(i);
NodeList pCode = e.getElementsByTagName("tpr");
Element line = (Element)pCode.item(0);
resultTable[i] = getCharacterDataFromElement(line);
}
}
catch(Exception e) {
e.printStackTrace();
}
return resultTable;
}
public static String getCharacterDataFromElement(Element e) {
Node child = e.getFirstChild();
if (child instanceof CharacterData) {
CharacterData cData = (CharacterData) child;
return cData.getData();
}
return "null";
}
public static String getParamByXPath(String xmlString, String expression) {
String ret = "";
XPath xpath = XPathFactory.newInstance().newXPath();
try{
DocumentBuilderFactory dBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dBuilderFactory.newDocumentBuilder();
InputSource iSource = new InputSource();
iSource.setCharacterStream(new StringReader(xmlString));
Document doc = dBuilder.parse(iSource);
XPathExpression exp = xpath.compile(expression);
Object result = exp.evaluate(doc);
if (result instanceof String) ret = (String)result;
else if (result instanceof Boolean) ret = result.toString();
else if (result instanceof Double) ret = result.toString();
else if (result instanceof NodeList) {
NodeList list = (NodeList) result;
Node node = list.item(0);
Log.d("LIST", Integer.toString(list.getLength()));
ret = node.getTextContent();
}
}catch(Exception e) {
e.printStackTrace();
}
return ret;
}
私はこの行でメソッドを呼び出しています:
String loc1 = XMLParser.getParamByXPath(service, "//@d");
他のクエリを使用していますが、結果は常に最初の要素です...何が欠けている可能性がありますか?