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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:07:22+00:00 2026-05-27T04:07:22+00:00

I am trying to parse the following xml. I can access the WeekNumber easily

  • 0

I am trying to parse the following xml. I can access the WeekNumber easily but cannot access the children for EmployeeRatesLevelA and EmployeeRatesLevelB. The goal is to save these to a class, DataSet with fields WeekNumber and ArrayLists, EmployeeRatesLevelA and EmployeeRatesLevelB.
Thanks.

<DataSet ActiveFrom="2011/04/06">  
    <WeekNumber>8</WeekNumber>  
    <EmployeeRatesLevelA>  
        <Rate>0</Rate>  
        <Rate>0.12</Rate>  
    </EmployeeRatesLevelA>  
    <EmployeeRatesLevelB>  
        <Rate>0.15</Rate>  
        <Rate>0.20</Rate>  
    </EmployeeRatesLevelB>  
</DataSet>  


    Document doc = loadXml("data.xml");  
    NodeList nodeList = doc.getElementsByTagName("DataSet");  
    for (int i = 0; i < nodeList.getLength(); i++) {  
        Node node = nodeList.item(i);  
        if (node.getNodeType() == Node.ELEMENT_NODE) {  
            Element element = (Element) node;  
            NodeList weekNumberList = element.getElementsByTagName("WeekNumber");  
            Element weekElement = (Element) weekNumberList.item(0);  
            NodeList textElementList = weekElement.getChildNodes();
            System.out.println("Weeknumber:"+ ((Node)textElementList.item(0)).getNodeValue().trim());
    }

    public static Document loadXml(String file) {
        try {
           return (DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File(file)));
        } catch (SAXException e) {
        e.printStackTrace();
        } catch (IOException e) {
        e.printStackTrace();
        } catch (ParserConfigurationException e) {
        e.printStackTrace();
        }
        return null;
    }

This gives me the Weeknumber but I am unable to access EmployeeRatesLevelA and EmployeeRatesLevelB.

Would like to learn other cool stuff but as I am new to Java and the xml document is really small, DOM should suffice.

  • 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-05-27T04:07:23+00:00Added an answer on May 27, 2026 at 4:07 am

    If you want to use DOM, I suggest you to start by writing some helper classes to make your work easier. Here is one I written recently for my personal use.

    Let’s start with the helper classes in package xml.utils

    XmlException.java

    package xml.utils;
    
    public class XmlException extends Exception {
        private static final long serialVersionUID = 1L;
    
        public XmlException(String message, Throwable cause)  {
            super(message, cause);
        }
    
        public XmlException(String message)  {
            super(message);
        }
    
        public XmlException(Throwable cause)  {
            super(cause);
        }
    }
    

    XmlDocument.java

    package xml.utils;
    
    import java.io.File;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.io.Writer;
    
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.ParserConfigurationException;
    import javax.xml.transform.Transformer;
    import javax.xml.transform.TransformerConfigurationException;
    import javax.xml.transform.TransformerException;
    import javax.xml.transform.TransformerFactory;
    import javax.xml.transform.dom.DOMSource;
    import javax.xml.transform.stream.StreamResult;
    
    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.xml.sax.SAXException;
    
    public class XmlDocument {
        private Document document;
    
        public XmlNode parse(InputStream is) throws XmlException {
            try {
                DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
                document = dBuilder.parse(is);
                document.getDocumentElement().normalize();
    
                XmlNode node = new XmlNode(document.getDocumentElement());
                return node;
            } catch (ParserConfigurationException e) {
                throw new XmlException("Error in configuration of XML parser", e);
            } catch (SAXException e) {
                throw new XmlException("Error in parsing XML document", e);
            } catch (IOException e) {
                throw new XmlException("Error in reading InputStream", e);
            }
        }
    
        public XmlNode parse(String uri) throws XmlException {
            try {
                DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
                document = dBuilder.parse(uri);
                document.getDocumentElement().normalize();
    
                XmlNode node = new XmlNode(document.getDocumentElement());
                return node;
            } catch (ParserConfigurationException e) {
                throw new XmlException("Error in configuration of XML parser", e);
            } catch (SAXException e) {
                throw new XmlException("Error in parsing XML document", e);
            } catch (IOException e) {
                throw new XmlException("Error in opening URI", e);
            }
        }
    
        public XmlNode parse(File file) throws XmlException {
            try {
                DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
                document = dBuilder.parse(file);
                document.getDocumentElement().normalize();
    
                XmlNode node = new XmlNode(document.getDocumentElement());
                return node;
            } catch (ParserConfigurationException e) {
                throw new XmlException("Error in configuration of XML parser", e);
            } catch (SAXException e) {
                throw new XmlException("Error in parsing XML document", e);
            } catch (IOException e) {
                throw new XmlException("Error in opening file", e);
            }
        }
    
        public void write(OutputStream os, XmlNode node) throws XmlException {
            try {
                if (document == null) {
                    document = createNewDocument();
                }
                document.appendChild(node.getNode());
    
                // write the content into xml file
                TransformerFactory transformerFactory = TransformerFactory.newInstance();
                Transformer transformer = transformerFactory.newTransformer();
                DOMSource source = new DOMSource(document);
                StreamResult result = new StreamResult(os);
    
                transformer.transform(source, result);
            } catch (TransformerConfigurationException e) {
                throw new XmlException("Error in configuration of XML writer", e);
            } catch (TransformerException e) {
                throw new XmlException("Error in writing XML", e);
            }
        }
    
        public void write(File file, XmlNode node) throws XmlException {
            try {
                if (document == null) {
                    document = createNewDocument();
                }
                document.appendChild(node.getNode());
    
                // write the content into xml file
                TransformerFactory transformerFactory = TransformerFactory.newInstance();
                Transformer transformer = transformerFactory.newTransformer();
                DOMSource source = new DOMSource(document);
                StreamResult result = new StreamResult(file);
    
                transformer.transform(source, result);
            } catch (TransformerConfigurationException e) {
                throw new XmlException("Error in configuration of XML writer", e);
            } catch (TransformerException e) {
                throw new XmlException("Error in writing XML", e);
            }
        }
    
    
    
        public void write(Writer writer, XmlNode node) throws XmlException {
            try {
                if (document == null) {
                    document = createNewDocument();
                }
                document.appendChild(node.getNode());
    
                // write the content into xml file
                TransformerFactory transformerFactory = TransformerFactory.newInstance();
                Transformer transformer = transformerFactory.newTransformer();
                DOMSource source = new DOMSource(document);
                StreamResult result = new StreamResult(writer);
    
                transformer.transform(source, result);
            } catch (TransformerConfigurationException e) {
                throw new XmlException("Error in configuration of XML writer", e);
            } catch (TransformerException e) {
                throw new XmlException("Error in writing XML", e);
            }
        }
    
        private Document createNewDocument() throws XmlException {
            try {
                DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
                return dBuilder.newDocument();
            } catch (ParserConfigurationException e) {
                throw new XmlException("Error in configuration of XML parser", e);
            }
        }
    
        public XmlNode createNode(String nodeName) throws XmlException {
            if (document == null) {
                document = createNewDocument();
            }
            XmlNode node = new XmlNode(this, document.createElement(nodeName));
            return node;
        }
    
        XmlNode createNode(String nodeName, String nodeValue) throws XmlException {
            if (document == null) {
                document = createNewDocument();
            }
            Element node = document.createElement(nodeName);
            node.appendChild(document.createTextNode(nodeValue));
    
            return new XmlNode(this, node);
        }
    }
    

    XmlNode.java

    package xml.utils;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.w3c.dom.Element;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    
    public class XmlNode {
        private Element node;
        private XmlDocument parent;
    
        XmlNode(Element node) {
            this.node = node;
            this.parent = null;
        }
    
        XmlNode(XmlDocument parent, Element node) {
            this.node = node;
            this.parent = parent;
        }
    
        Node getNode() {
            return node;
        }
    
            public String getNodeValue() {
                return node.getTextContent();
            }
    
        public XmlDocument getParent() {
            return parent;
        }
    
        public void setParent(XmlDocument parent) {
            this.parent = parent;
        }
    
        public List<XmlNode> getChildNodes() {
            List<XmlNode> list = new ArrayList<XmlNode>();
            NodeList nodeList = node.getChildNodes();
            for (int i = 0; i < nodeList.getLength(); i++) {
                Node n = nodeList.item(i);
                if (n.getNodeType() == Node.ELEMENT_NODE) {
                    list.add(new XmlNode((Element) n));
                }
            }
    
            return list;
        }
    
        public XmlNode getFirstChild() {
            return getChildNodes().get(0);
        }
    
        public XmlNode getLastChild() {
            List<XmlNode> childs = getChildNodes();
            if (childs.size() == 0)
                return null;
    
            return childs.get(childs.size() - 1);
        }
    
        public List<XmlNode> getNodesByTagName(String tagName) {
            List<XmlNode> list = new ArrayList<XmlNode>();
            NodeList nodeList = node.getElementsByTagName(tagName);
            for (int i = 0; i < nodeList.getLength(); i++) {
                Node n = nodeList.item(i);
                if (n.getNodeType() == Node.ELEMENT_NODE) {
                    list.add(new XmlNode((Element) n));
                }
            }
    
            return list;
        }
    
        public XmlNode getFirstNodeByTagName(String tagName) {
            return getNodesByTagName(tagName).get(0);
        }
    
        public String getTagValue(String tagName) throws XmlException {
            NodeList tagList = node.getElementsByTagName(tagName);
            if (tagList.getLength() == 0)
                throw new XmlException("Tag: '" + tagName + "' not present");
    
            NodeList nlList = tagList.item(0).getChildNodes();       
            Node nValue = (Node) nlList.item(0);
    
            return nValue.getNodeValue();
        }
    
        public String getAttributeValue(String attributeName) {
            return node.getAttribute(attributeName);
        }
    
        public String getNodeName() {
            return node.getTagName();
        }
    
        public void setAttribute(String name, String value) throws XmlException {
            if (parent == null) 
                throw new XmlException("Parent node not present.");
    
            node.setAttribute(name, value);
        }
    
        public void setTag(String name, String value) throws XmlException {
            if (parent == null) 
                throw new XmlException("Parent node not present.");
    
            XmlNode xmlNode = parent.createNode(name, value);
            node.appendChild(xmlNode.node);
        }
    
        public void addChildNode(XmlNode xmlNode) throws XmlException {
            if (parent == null) 
                throw new XmlException("Parent node not present.");
    
            node.appendChild(xmlNode.node);
        }
    
        public XmlNode addChildNode(String nodeName) throws XmlException {
            if (parent == null) 
                throw new XmlException("Parent node not present.");
    
            XmlNode child = parent.createNode(nodeName);
            node.appendChild(child.getNode());
    
            return child;
        }
    }
    

    Now the DataSet.java and Main.java are as follows:

    DataSet.java

    package tests;
    
    import java.io.File;
    import java.util.ArrayList;
    import java.util.List;
    import xml.utils.XmlDocument;
    import xml.utils.XmlException;
    import xml.utils.XmlNode;
    
    public class DataSet {
        private int weekNumber;
        private List<Float> employeeRatesLevelA;
        private List<Float> employeeRatesLevelB;
    
        public DataSet(File xml) throws XmlException {
            employeeRatesLevelA = new ArrayList<Float>();
            employeeRatesLevelB = new ArrayList<Float>();
    
            loadFromXml(xml);
        }
    
        private void loadFromXml(File xml) throws XmlException {
            XmlDocument document = new XmlDocument();
            XmlNode root = document.parse(xml);
    
            weekNumber = Integer.parseInt(root.getTagValue("WeekNumber"));
    
            XmlNode ratesLevelNode = root.getNodesByTagName("EmployeeRatesLevelA").get(0);
            List<XmlNode> rates = ratesLevelNode.getNodesByTagName("Rate");
            for (XmlNode xmlNode : rates) {
                employeeRatesLevelA.add(Float.parseFloat(xmlNode.getNodeValue()));
            }
    
            ratesLevelNode = root.getNodesByTagName("EmployeeRatesLevelB").get(0);
            rates = ratesLevelNode.getNodesByTagName("Rate");
            for (XmlNode xmlNode : rates) {
                employeeRatesLevelB.add(Float.parseFloat(xmlNode.getNodeValue()));
            }
        }
    
        public void display() {
            System.out.println("WeekNumber: " + weekNumber);
            System.out.println("Level A");
            for (Float rate : employeeRatesLevelA) {
                System.out.println("\tRate: " + rate);
            }
    
            System.out.println("Level B");
            for (Float rate : employeeRatesLevelB) {
                System.out.println("\tRate: " + rate);
            }
        }
    }
    

    Main.java

    package tests;
    
    import java.io.File;
    import java.io.IOException;
    import org.xml.sax.SAXException;
    import xml.utils.XmlException;
    
    public class Main {
        public static void main(String[] args) throws SAXException, IOException, XmlException {
            File dataFile = new File("/home/jomit/data.xml");
            DataSet dataSet = new DataSet(dataFile);
            dataSet.display();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to parse the following xml to punycode but it's not working
I have the following XML code that I'm trying to parse, but I'm sure
i'm trying to parse this xml file but i can't find a way to
I'm trying to validate an XML file, but I get the following error: Can
I am trying to parse the following XML with javascript: <?xml version='1.0' encoding='UTF-8'?> <ResultSet>
So I'm trying to parse the following XML document with C#, using System.XML: <root
Trying to parse some XML but apparently this is too much for a lazy
I am trying to parse the XML file in R, so that I can
I'm new to xml parsing, I am trying to parse the following xml file
i am trying to read a xml file with following tag, but the sax

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.