简体   繁体   中英

XQUERY JAVA not working

I'm getting some attributes from a complex XML file:

<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>

But my XQUERY only gives me the first attribute and only that using this class:

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;     
}

I'm invoking the method with this line:

String loc1 = XMLParser.getParamByXPath(service, "//@d");

I have been using other Querys, but the result is always the first element... what could I be missing?

The //@d XPath will return a list of all @d attribute nodes in your document.

Your Java code:

  else if (result instanceof NodeList)    {
        NodeList list = (NodeList) result;

        Node node = list.item(0); // <-- THIS ONE HERE

        Log.d("LIST", Integer.toString(list.getLength()));
        ret = node.getTextContent();
    }

will then take the first one ( list.item(0) ) and that's what you're getting.

It looks like your "issue" is with the Java logic, not the XPath.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM