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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T20:07:59+00:00 2026-06-08T20:07:59+00:00

Let us say I have an xml tag like: <test val=val1 >Test XML</test> I

  • 0

Let us say I have an xml tag like:

<test val="val1 ">Test XML</test>

I want to parse the value “val1” whenever <test> tag occurs in the file so that I can save it to an array.

  • 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-08T20:08:01+00:00Added an answer on June 8, 2026 at 8:08 pm

    You can load the XML from the file and select all elements with tag name test and extract attribute value from it.

    Basically:

    doc.getElementsByTagName("test") in order to select all "test" elements, and for each element found you can select its attribute named "val" with node.getAttributes().getNamedItem("val") and from this one, if not null just getTextContent() to take its text content.


    A sample code could be something like:

    // temporary result list just to store the values found
    ArrayList<String> result = new ArrayList<String>();
    
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
    Document doc = documentBuilder.parse(new ByteArrayInputStream("<document><test val=\"val1\">Test XML</test><test val=\"val2\">Test XML</test><test val=\"val3\">Test XML</test></document>".getBytes()));
    // you could use something like: documentBuilder.parse(new FileInputStream("fileName.ext")); instead
    
    // get test nodes and iterate over it checking test attribute
    NodeList elements = doc.getElementsByTagName("test");
    for (int i = 0; i < elements.getLength(); i++) {
        Node node = elements.item(i);
        Node attribute = node.getAttributes().getNamedItem("val");
        if (attribute != null) {
            result.add(attribute.getTextContent());
        }
    }
    
    // get result values in a string array (if collection is preferred use result instead)
    String[] array = result.toArray(new String[] {});
    
    // just print it in a readable format
    System.out.println(Arrays.toString(array));
    

    This will print:

    [val1, val2, val3]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have an xml file with this part: <tag name=key value=${tag.val}/> This
Let's say I have an empty XML file like so: <root></root> And I want
Let's say that I have something like this in my web.xml file. <filter name=foo>
Let's say I have a line in an XML file like the following (derived
Let's say I have an XML file that looks like this: <a id=x> <b>
Let's say I have an xml file like this: <root> <a><b><c> w </c></b></a> <x><y><z>
Let's just say I have an XML file that looks like this: <?xml version=1.0
Let's say I have the following XML file <a id=123> <b type=foo value=1 />
Let's say i have a xml file with a tag named which contains the
Let's say I have the following XML: <Account> <AccountExpirationDate>6/1/2009</AccountExpirationDate> </Account> I want to use

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.