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

  • Home
  • SEARCH
  • 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 6601629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:46:44+00:00 2026-05-25T18:46:44+00:00

So I have been asking questions here trying to get the answer for myself

  • 0

So I have been asking questions here trying to get the answer for myself but I just cant get it to work without running into a new error. If anyone can help me I would appreciate it. I want to replace this portion

"<a>\n" + 
"<b>\n" + 
"<c id=\"00001\" time=\"1:00\" day=\"Friday\" name1=\"John\" name2=\"Mary\"></c>\n" + 
"<c id=\"00002\" time=\"2:00\" day=\"Monday\" name1=\"Ed\" name2=\"Kate\"></c>\n" + 
"<c id=\"00003\" time=\"3:00\" day=\"Sunday\" name1=\"Mary\" name2=\"Ed\"></c>\n" + 
"<c id=\"00004\" time=\"4:00\" day=\"Friday\" name1=\"Kate\" name2=\"John\"></c>\n" + 
"</b>\n" + 
"</a>"

with a XML url instead, as that information will be pulled from a server as the data changes.

Here is the source as you can see what I am trying accomplish once I have the data from the xml file. It works fine as it is, but whenever I try and implement a url as the InputSource I get tons of errors that no matter what ive tried does not resolve the problem.

package com.newxpath;

import java.io.StringReader;

import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;

public class NewxpathActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    InputSource xml = new InputSource(new StringReader("<a>\n" + 
            "<b>\n" + 
            "<c id=\"00001\" time=\"1:00\" day=\"Friday\" name1=\"John\" name2=\"Mary\"></c>\n" + 
            "<c id=\"00002\" time=\"2:00\" day=\"Monday\" name1=\"Ed\" name2=\"Kate\"></c>\n" + 
            "<c id=\"00003\" time=\"3:00\" day=\"Sunday\" name1=\"Mary\" name2=\"Ed\"></c>\n" + 
            "<c id=\"00004\" time=\"4:00\" day=\"Friday\" name1=\"Kate\" name2=\"John\"></c>\n" + 
            "</b>\n" + 
            "</a>"));

    String name = "Ed";

    XPath xpath = XPathFactory.newInstance().newXPath();
    String expr = String.format("//a/b/c[@name2='%s']", name);
    Node c = null;
    try {
        c = (Node) xpath.evaluate(expr, xml, XPathConstants.NODE);
    } catch (XPathExpressionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    NamedNodeMap attribs = c.getAttributes();
    String id = attribs.getNamedItem("id").getNodeValue();
    String time = attribs.getNamedItem("time").getNodeValue();
    // etc.

    EditText id2 = (EditText) findViewById(R.id.id2);
    EditText time2 = (EditText) findViewById(R.id.time2);

    id2.setText(String.valueOf(id));
    time2.setText(String.valueOf(time));

}
}
  • 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-05-25T18:46:44+00:00Added an answer on May 25, 2026 at 6:46 pm

    You probably need to add the INTERNET permission to your AndroidManifest.xml file (I assume from the Android imports that this is taking place on Android). Otherwise I don’t see why this wouldn’t work. I copied your XML to the URL http://pastebin.com/raw.php?i=RF8cL5YZ and then ran it against the following code at a command line and it worked just fine.

    import java.io.StringReader;
    
    import javax.xml.xpath.XPath;
    import javax.xml.xpath.XPathConstants;
    import javax.xml.xpath.XPathExpressionException;
    import javax.xml.xpath.XPathFactory;
    
    import org.w3c.dom.NamedNodeMap;
    import org.w3c.dom.Node;
    
    import org.xml.sax.InputSource;
    import java.net.*;
    
    public class test
    {
        public static void main(String[] args) throws Exception
        {
        URL url = new URL("http://pastebin.com/raw.php?i=RF8cL5YZ");
        InputSource xml = new InputSource(url.openStream());
        String name = "Ed";
    
        XPath xpath = XPathFactory.newInstance().newXPath();
        String expr = String.format("//a/b/c[@name2='%s']", name);
        Node c = null;
        try {
            c = (Node) xpath.evaluate(expr, xml, XPathConstants.NODE);
        } catch (XPathExpressionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    
        NamedNodeMap attribs = c.getAttributes();
        String id = attribs.getNamedItem("id").getNodeValue();
        String time = attribs.getNamedItem("time").getNodeValue();
        // etc.
    
        System.out.println("["+String.valueOf(id)+"]["+String.valueOf(time)+"]");
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been asking similar questions here today, but I'm seeing I think the issue
I have been asking myself this question for a long time now. Thought of
Hey guys, lately I have been asking quite a few questions about memory management
Sorry guys, I've been running a mock asking questions on how to integrate wikipedia
For the last two days, I have been asking questions on rank queries in
I have been asking a lot of questions about a project I have been
I'm making a SAAS and I've been asking a slew of questions on here
Have been studying the file system related classes of Adobe AIR 1.5, but so
Have been struggling with Javascript closure for a while trying to wrap brain around
I've seen a couple of similar questions to this, but haven't been able 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.