Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7781135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:07:47+00:00 2026-06-01T19:07:47+00:00

My goal is executing an XQuery using XPath. My XML file is: <?xml version=1.0

  • 0

My goal is executing an XQuery using XPath.

My XML file is:

<?xml version="1.0" encoding="UTF-8"?>
<postes>
    <poste>
        <gouvernourat>Kairouan</gouvernourat>
        <ville>Kairouan sud</ville>
        <cp>3100</cp>
    </poste>
    <poste>
        <gouvernourat>Tunis</gouvernourat>
        <ville>Ghazela</ville>
        <cp>1002</cp>
    </poste>
</postes>

My Java code is:

package xmlparse;


import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class QueryXML {
    public void query() throws ParserConfigurationException, SAXException,
            IOException, XPathExpressionException {
        // Standard of reading a XML file
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        DocumentBuilder builder;
        Document doc = null;
        XPathExpression expr = null;
        builder = factory.newDocumentBuilder();
        doc = builder.parse("a.xml");  //C:\\Users\\aymen\\Desktop\\

        // Create a XPathFactory
        XPathFactory xFactory = XPathFactory.newInstance();

        // Create a XPath object
        XPath xpath = xFactory.newXPath();

        // Compile the XPath expression
        expr = xpath.compile("/postes/poste[gouvernourat='Tunis']/ville/text()");
        // Run the query and get a nodeset
        Object result = expr.evaluate(doc, XPathConstants.NODESET);

        // Cast the result to a DOM NodeList
        NodeList nodes = (NodeList) result;
        for (int i=0; i<nodes.getLength();i++){
            System.out.println(nodes.item(i).getNodeValue());
        }
    }

    public static void main(String[] args) throws XPathExpressionException, ParserConfigurationException, SAXException, IOException {
        QueryXML process = new QueryXML();
        process.query();
    }
}

When I launch this Java code the result is displayed on the console correctly (System.out.println).

But if I copy this code to my Android application and change System.out.println(nodes.item(i).getNodeValue()); to Text2.setText(nodes.item(i).getNodeValue()); (I have a TextView named Text2)

When I execute the code and I click the button the TextView stays empty (No error for Force Close)

Thank you in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T19:07:48+00:00Added an answer on June 1, 2026 at 7:07 pm

    Attribute names needs to start with ‘@’ while using XPath in Android.
    So change

    [gouvernourat='Tunis']
    

    To

    [@gouvernourat='Tunis']
    

    Refer http://developer.android.com/reference/javax/xml/xpath/package-summary.html for details.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Goal: Create Photomosaics programmatically using .NET and C#. Main reason I'd like to do
Goal : Our application is built using multiple types (e.g. Person, PersonSite(ICollection), Site -
Current situation I'm executing an external application (by using Runtime.getRuntime().exec ... example here )
I am using antrun plugin in my maven build to replace a token @version@
I would like to have a Maven goal trigger the execution of a java
It doesn't seem to possible to override a plugin execution's goal definition. Let say
Goal Java client for Yahoo's HotJobs Resumé Search REST API . Background I'm used
Goal: to programmatically determine the sizes (in bytes) of the fields of a class.
goal: I have the string 1234432144 I want to only replace the first 2
Goal Pass images generated by one process efficiently and at very high speed to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.