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 7186009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:32:28+00:00 2026-05-28T18:32:28+00:00

I am trying to parse a dom element. Element element: <?xml version=1.0 encoding=UTF-8?> <feed

  • 0

I am trying to parse a dom element.

Element element:

<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <id>http://X/feed2</id>
  <title>Sample Feed</title>
  <entry>
    <id>http://X/feed2/104</id>
    <title>New Title</title>
  </entry>
</feed>

I am trying to fetch the following entry:

<entry>
  <id>http://top.cs.vt.edu/libx2/vsony7@vt.edu/feed2/104</id>
  <title>New Title</title>
</entry>

I am parsing the xml by using the xpath:

“/atom:feed/atom:entry[atom:id=\”http://X/feed2/104\”]”

But, I get an exception when I try to parse Dom Element. Can someone suggest a simple approach to achieve this in Java?

Please see my full code:

public static parseXml() {
        String externalEntryIdUrl = "http://theta.cs.vt.edu/~rupen/thirtylibapps/137";
        String externalFeedUrl = StringUtils.substringBeforeLast(externalEntryIdUrl, "/");
        try {
            URL url = new URL(externalFeedUrl);
            InputStream externalXml = new BufferedInputStream(url.openStream());
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(externalXml);
            Element externalFeed = doc.getDocumentElement();
            String atomNameSpace = "xmlns:atom=\"http://www.w3.org/2005/Atom\"";
            String entryIdPath = String.format("//%s:entry[%s:id=%s]", atomNameSpace, atomNameSpace, externalEntryIdUrl);
            Element externalEntry = (Element) XPathSupport.evalNode(entryIdPath, externalFeed);
        } catch (Exception ex) {
            // Throw exception
        }
    }

static synchronized Node evalNode(String xpathExpr, Node node) {
    NodeList result = evalNodeSet(xpathExpr, node);
    if (result.getLength() > 1)
        throw new Error ("More than one node for:" + xpathExpr);
    else if (result.getLength() == 1)
        return result.item(0);
    else
        return null;
}

static synchronized NodeList evalNodeSet(String xpathExpr, Node node) {
        try {
                static XPath xpath = factory.newXPath();
                xpath.setNamespaceContext(context);

                static NamespaceContext context = new NamespaceContext() {
                    private Map<String, String> prefix2URI = new HashMap<String, String>();
                    {
                        prefix2URI.put("libx", "http://libx.org/xml/libx2");
                        prefix2URI.put("atom", "http://www.w3.org/2005/Atom");
                    }
                };

            XPathExpression expr = xpath.compile(xpathExpr);
            Object result = expr.evaluate(node, XPathConstants.NODESET);
            return (NodeList)result;
        } catch (XPathExpressionException xpee) {
            throw new Error ("An xpath expression exception: " + xpee);
        }
    }

SEVERE: >>java.lang.Error: An xpath expression exception: javax.xml.xpath.XPathExpressionException

  • 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-28T18:32:29+00:00Added an answer on May 28, 2026 at 6:32 pm

    I figured that I didn’t set the namespace awareness while fetching the xml from a URL.

    So,

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    

    Doing so fixes my issue. Without doing this, setting the namespace context for XPathFactory instance while parsing the xml as shown in my example doesn’t work by itself.

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

Sidebar

Related Questions

I have the following xml file: <?xml version=1.0 encoding=UTF-8?> <c1> <c2 id=0001 n=CM urlget=/at/CsM
I'm trying to parse UTF-8 XML file and save some parts of it to
I am trying to parse XML using PHP DOM and then insert this data
I am trying to parse a xml file that i have (using javascript DOM).
I am trying to use DOM Parser in Java to parse a small XML
I'm trying to parse a bunch of xml files with the library xml.dom.minidom, to
I need to parse this xml. I am trying dom parser. I am not
Trying to parse some XML but apparently this is too much for a lazy
I'm trying to parse objects to XML in Delphi, so I read about calling
I am trying to parse the following XML file in Java and to store

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.