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

  • Home
  • SEARCH
  • 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 9167873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:29:50+00:00 2026-06-17T15:29:50+00:00

I have a XML file that I am trying to read and transform into

  • 0

I have a XML file that I am trying to read and transform into objects. I want to transform and put all the locations in a array filled with Location objects wich are defined by a film id, a date and a amount.

Here is my XML file :

Here is my code to scan the location XML section :

 public void findLocations() throws ParseException {

    NodeList nList = document.getElementsByTagName("location");
    Location[] locations = new Location[nList.getLength()];
    for (int temp = 0; temp < nList.getLength(); temp++) {
        Node nNode = nList.item(temp);

        if (nNode.getNodeType() == Node.ELEMENT_NODE) {
           Element eElement = (Element) nNode;
              locations[temp] = new Location(getTagValue("filmid", eElement), dateChanger(getTagValue("date", eElement)), getTagValue("amount", eElement));
         System.out.println(locations[temp].getAmount()); //Outputs the good values.
        }

    }
 System.out.println(locations[0].getAmount()); //Output : 5$
 System.out.println(locations[1].getAmount()); //Output : 5$
 System.out.println(locations[2].getAmount()); //Output : 5$
}


private static String getTagValue(String sTag, Element eElement) {

    NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();
    Node nValue = (Node) nlList.item(0);
    return nValue.getNodeValue();
}

The problem seems that my array is getting filled 3 times with the same location and ends up filled 3 time with the last location. The objects otherwise are well formed, so I imagine I got that part right.

  • 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-17T15:29:51+00:00Added an answer on June 17, 2026 at 3:29 pm

    You could use XPath instead…

    public class TestXML03 {
    
        public static void main(String[] args) {
    
            try {
                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                factory.setNamespaceAware(false);
                DocumentBuilder builder = factory.newDocumentBuilder();
                Document xmlDoc = builder.parse(new File("Test.xml"));
    
                Node root = xmlDoc.getDocumentElement();
    
                XPathFactory xFactory = XPathFactory.newInstance();
                XPath xPath = xFactory.newXPath();
    
                XPathExpression xExpress = xPath.compile("/file/location");
                NodeList nodes = (NodeList) xExpress.evaluate(root, XPathConstants.NODESET);
    
                System.out.println("Found " + nodes.getLength() + " location nodes");
                System.out.println("");
    
                for (int index = 0; index < nodes.getLength(); index++) {
                    Node node = nodes.item(index);
                    xExpress = xPath.compile("filmid");
                    Node filmIDNode = (Node) xExpress.evaluate(node, XPathConstants.NODE);
                    System.out.println(filmIDNode.getNodeName() + " = " + filmIDNode.getTextContent());
    
                    xExpress = xPath.compile("date");
                    Node dateNode = (Node) xExpress.evaluate(node, XPathConstants.NODE);
                    System.out.println(dateNode.getNodeName() + " = " + dateNode.getTextContent());
    
                    xExpress = xPath.compile("amount");
                    Node amountNode = (Node) xExpress.evaluate(node, XPathConstants.NODE);
                    System.out.println(amountNode.getNodeName() + " = " + amountNode.getTextContent());
    
                    System.out.println("");
                }
    
            } catch (Exception exp) {
                exp.printStackTrace();
            }
        }
    }
    

    Which outputs…

    Found 3 location nodes
    
    filmid = 100
    date = 2013-01-11
    amount = 4.00$
    
    filmid = 200
    date = 2013-01-13
    amount = 9.00$
    
    filmid = 334
    date = 2013-01-23
    amount = 5.00$
    

    Updated After Feed-back

    The Location class is maintaining a static reference to it’s class fields, this means that changing the value for the field will change it for all instances of that class.

    Remove the static references and it should solve the problem.

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

Sidebar

Related Questions

I have an XML file that I'm trying to read from here , and
I have a really simple XML file that I'm trying to read, but I
I am trying to read in an XML file that I have saved to
I have an XML file that I'm trying to serialize into an object. Some
I have an XML file that I'm trying to parse with Linq-to-XML. One of
So I have an XML file that I am trying to loop through in
i have a coode that are trying to get the id of xml file
i have a simple xml file in a wcf service that i am trying
Have been trying to encrypt an xml file to a string so that I
I have a very simple xml file that I am trying to access: <article>

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.