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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:29:45+00:00 2026-06-18T12:29:45+00:00

I have a simple XML, and I want to get the attributes. There are

  • 0

I have a simple XML, and I want to get the attributes. There are a few examples on the web, but I still dont understand why I get 17 when I see only 4. I even try to count locations where I think text could be, but still I don’t get that number unless is the length of the output . Which leads me to not know how to get the attribute name of all Tag3.

<?xml version="1.0" encoding="UTF-8"?>
<tag1 xmlns="something">
    <xxxxxx-Set>
        <tag3 Name="a"/>
        <tag3 Name="b"/>
        <tag3 Name="c"/>
        <tag3 Name="d"/>
    </xxxxxx-Set>
    <tagB>
        <tag3 Name="a"/>
        <tag3 Name="b"/>
        <tag3 Name="c"/>
        <tag3 Name="d"/>
    </tagB>
</tag1>

This is my java code:

import java.io.File;
import java.util.Arrays;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;


public class ParseXML {


    public static void main(String[] args) {
        try {

            File test= new File("test.xml");
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(test);

            NodeList tagAs= doc.getElementsByTagName("xxxxxx-Set").item(0).getChildNodes(); //should be all the tag3 elements?

            for(int i = 0; i < tagAs.getLength(); i++)    {
                System.out.println(tagAs);
                System.out.println(i);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

Note: adding .getAttributes().getNamedItem(“Name”).getNodeValue() to the print statement gives me null exception.
And the output is:

[xxxxxx-Set: null]
0
[xxxxxx-Set: null]
1
...
[xxxxxx-Set: null]
16
  • 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-18T12:29:47+00:00Added an answer on June 18, 2026 at 12:29 pm

    If you want to take all your Name attributes (it’s better to name them with lower case), use next approach:

    Element xSet = (Element) doc.getElementsByTagName("xxxxxx-Set").item(0);
    NodeList xSetTags = xSet.getElementsByTagName("tag3");
    
    for(int i = 0; i < xSetTags.getLength(); i++)    {
        Element tag3 = (Element) xSetTags.item(i);
        System.out.println(tag3.getAttribute("Name"));
    }
    

    I made it using org.w3c.dom.Element class. It’s not the best idea to work with org.w3c.dom.Node, because this class represents not only xml elements, but attributes, comments and other too. Look documentation to get difference between Node and Element classes.

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

Sidebar

Related Questions

I have a simple xml document and I want to get the count of
I have a simple xml file and I want to remove everything before the
I have a really simple XML file that I'm trying to read, but I
OK so I have a few operations for my REST Web Service. There is
My problem is that I want to create xml tree and get a simple
Let's assume I have a simple XML file: <data> <text>Hello world!&lt;br&gt;Nice to see you
I have the following very simple XML file and I want to quickly parse
I have a simple xml of users. I have a StudentId , I just
I have really simple XML (HTML) parsing ANTLR grammar: wiki: ggg+; ggg: tag |
I have a simple XML extraction issue that should be solvable with straight PHP

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.