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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:13:59+00:00 2026-06-12T20:13:59+00:00

How do you parse the same name tag in xml using dom parser java?

  • 0

How do you parse the same name tag in xml using dom parser java?

I have the following xml file that I would like to parse using the dom parser in java.

<?xml version="1.0"?>
    <GameWorld>
        <player>
            <playerID>1</playerID>
            <inventory>
                <item>cards</item>
                <item>notes</item>
                <item>dice</item>
            </inventory>
            <position>50 50 10 60</position>
            <room>offices</room>
        </player>
        <player>
            <playerID>2</playerID>
            <inventory>
                <item>notes</item>
                <item>dice</item>
                <item>cards</item>
            </inventory>
            <position>10 10 10</position>
            <room>security room</room>
        </player>
    </GameWorld>
  • 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-12T20:14:00+00:00Added an answer on June 12, 2026 at 8:14 pm
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(f);
    Element root = doc.getDocumentElement();
    NodeList nodeList = doc.getElementsByTagName("player");
    for (int i = 0; i < nodeList.getLength(); i++) {
      Node node = nodeList.item(i);
      // do your stuff
    }
    

    but I’d rather suggest to use XPath

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse(<uri_as_string>);
    XPathFactory xPathfactory = XPathFactory.newInstance();
    XPath xpath = xPathfactory.newXPath();
    XPathExpression expr = xpath.compile("/GameWorld/player");
    NodeList nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a semi xml formatted file that contains line with the following format:
I'm trying to configure IIS to parse a .xml file just like it would
I've got the following problem: I'm using the PHP XML DOM parser and when
Can scala's parser combinators parse xml and validate that the closing tags have the
I would like to parse the following text into formatted URLs with anchor tags:
I use NSXMLParser but i have some elements that they have the same name.
I want to add some data to a xml file like: <people> <person name=Jo>
I'm trying to parse a file from the web on Android using the DOM
I need help :D. I am using web service that returns similar xml file
i parsed my xml file successfully using sax parser.my doubt is how to convert

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.