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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:11:30+00:00 2026-05-14T03:11:30+00:00

TASK : I have an existing xml document (UTF-8) which uses xml namespaces and

  • 0

TASK : I have an existing xml document (UTF-8) which uses xml namespaces and xml schema. I need to parse to a particular element, append content (that also needs to use xml namespace prefixes) to this element and then write out the Document again.

which is the best XML parser library that I should be using for this TASK ?

I’ve seen a previous thread (Best XML parser for Java) but was not sure if dom4j or JDOM is any good for namespaces/xmlSchema and good support for UTF-8 characters.

Some parsers that seems like a task for
JDom
Dom4J
XOM
WoodStock

Any idea which one is the best ? 🙂 I use JDK 6 and would prefer NOT to use the built-in SAX/DOM facilities to do this job because that requires me to write too much code.

Would help to have some examples of doing such a task.

  • 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-14T03:11:31+00:00Added an answer on May 14, 2026 at 3:11 am

    Using JDOM, taking an InputStream and making it a Document:

    InputStream inputStream = (InputStream)httpURLConnection.getContent();
    DocumentBuilderFactory docbf = DocumentBuilderFactory.newInstance();
    docbf.setNamespaceAware(true);
    DocumentBuilder docbuilder = docbf.newDocumentBuilder();
    Document document = docbuilder.parse(inputStream, baseUrl);
    

    At that point, you have the XML in a Java object. Done. Easy.

    You can either use the document object and the Java API to just walk through it, or also use XPath, which I find easier (once I learned it).

    Build an XPath object, which takes a bit:

    public static XPath buildXPath() {
        XPathFactory factory = XPathFactory.newInstance();
        XPath xpath = factory.newXPath();
        xpath.setNamespaceContext(new AtomNamespaceContext());
        return xpath;
    }
    
    
    public class AtomNamespaceContext implements NamespaceContext {
    
        public String getNamespaceURI(String prefix) {
            if (prefix == null)
                throw new NullPointerException("Null prefix");
            else if ("a".equals(prefix))
                return "http://www.w3.org/2005/Atom";
            else if ("app".equals(prefix))
                return "http://www.w3.org/2007/app";
            else if ("os".equals(prefix))
                return "http://a9.com/-/spec/opensearch/1.1/";
            else if ("x".equals(prefix)) 
                return "http://www.w3.org/1999/xhtml";
            else if ("xml".equals(prefix))
                return XMLConstants.XML_NS_URI;
            return XMLConstants.NULL_NS_URI;
        }
    
        // This method isn't necessary for XPath processing.
        public String getPrefix(String uri) {
            throw new UnsupportedOperationException();
        }
    
        // This method isn't necessary for XPath processing either.
        public Iterator getPrefixes(String uri) {
            throw new UnsupportedOperationException();
        }
    }
    

    Then just use it, which (thankfully) doesn’t take much time at all:

    return Integer.parseInt(xpath.evaluate("/a:feed/os:totalResults/text()", document));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an Ant task which runs if the lock file is not existing.
I have a simple task. I have an existing project with a web service
I have been given the task of adding functionality to an existing IIS 6.0
Task: I have a camera mounted on the end of our assembly line, which
i have task which takes a parameter and has three modes of results Example
I'm using dhtmlx Gantt Chart UI component which have task list and graphical chart.
I have a task at work that involves converting legacy SGM files into XML.
I have a task to add logging functionality to the existing workflow based solution.
I have a task which I schedule to run periodically via ScheduledThreadPoolExecutor.scheduleAtFixedRate(task, rate, ...)
I have a long-time task implemented in Windows batch script need to be add

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.