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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:24:54+00:00 2026-06-08T12:24:54+00:00

I would like to check is code = ABC exists in my xml file

  • 0

I would like to check is code = "ABC" exists in my xml file using xPath.Can you please suggest me some methods for it?

<metadata>
 <codes class = "class1">
      <code code = "ABC">
            <detail "blah blah"/>
        </code>
  </codes>
  <codes class = "class2">
      <code code = "123">
            <detail "blah blah"/>
        </code>
  </codes>
 </metadata>

[EDIT]
I did following. It reuturns null.

            XPath xPath = XPathFactory.newInstance().newXPath();
            XPathExpression expr = xPath.compile("//codes/code[@ code ='ABC']");
            Object result = expr.evaluate(doc, XPathConstants.NODESET);

            NodeList nodes = (NodeList) result;
            for (int i = 0; i < nodes.getLength(); i++) {
                System.out.println("nodes: "+ nodes.item(i).getNodeValue()); 
            }
  • 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-08T12:24:55+00:00Added an answer on June 8, 2026 at 12:24 pm

    I don’t know how you tested your code because <detail "blah blah"/> is an incorrect xml construct it should be <detail x="blah blah"/> i.e. a name-value pair !!

    For the XPath Expression "//codes/code[@ code ='ABC']" the nodes.item(i).getNodeValue()) is going to be null because it will return an Element. See the below Javadoc comment:

    enter image description here

    A working sample:

    import java.io.ByteArrayInputStream;
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.xpath.XPath;
    import javax.xml.xpath.XPathConstants;
    import javax.xml.xpath.XPathExpression;
    import javax.xml.xpath.XPathFactory;
    
    import org.w3c.dom.Document;
    import org.w3c.dom.NodeList;
    
    public class Test 
    {
        public static void main(String[] args) throws Exception
        {
            Document doc = getDoc();
            XPath xPath = XPathFactory.newInstance().newXPath();
            XPathExpression expr = xPath.compile("//codes/code[@code ='ABC']");
            Object result = expr.evaluate(doc, XPathConstants.NODESET);
    
            NodeList nodes = (NodeList) result;
            System.out.println("Have I found anything? " + (nodes.getLength() > 0 ? "Yes": "No"));
    
            for (int i = 0; i < nodes.getLength(); i++) {
                System.out.println("nodes: "+ nodes.item(i).getNodeValue()); 
            }
    
        }
    
        private static Document getDoc() 
        {
            String xml = "<metadata>"+
                     "<codes class = 'class1'>"+
                          "<code code='ABC'>"+
                                "<detail x='blah blah'/>"+
                            "</code>"+
                      "</codes>"+
                      "<codes class = 'class2'>"+
                          "<code code = '123'>"+
                                "<detail x='blah blah'/>"+
                            "</code>"+
                      "</codes>"+
                     "</metadata>";
    
    
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    
            try {
    
                DocumentBuilder db = dbf.newDocumentBuilder();
                Document dom = db.parse(new ByteArrayInputStream(xml.getBytes()));
                return dom;
    
            }catch(Exception pce) {
                pce.printStackTrace();
            }
            return null;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to check if an object is defined or exists using C#.
Hi i would like to check if a property exists? This is my code
I would like to check out code from google code using the source, http://imedia.googlecode.com/svn/trunk/imedia-read-only
I would like to check tree system's status, I'm using the following code, but
I would like to check if the file exists and if not display noimage.gif.
I'm using PL/SQL, and I would like to check if a record exists in
I would like to check whether the request is XML od HTML. When HTML
I would like to check in a JAX-RS webservice request that valid XML was
I would like to check to a remote website if it contains some files.
I would like to check whether user checked box or not with php code.

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.