Read XML file with more than one value in one tag Using Java

java code to read xml file with attributes
parse xml with multiple root elements java
how to read xml file in java
java 8 xml parsing

I have my XML file like this

<Customers>
  <Customer Name="Test_91" Code="91" Purpose="Supplier" />
  <Customer Name="Test_92" Code="92" Purpose="Receiver" />
</Customers>

I need to read this file with java and I use code like this:

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

public class XMLReaderDOM {

    public static void main(String argv[]) {
        try {
            File fXmlFile = new File("Customers.xml");
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(fXmlFile);
            doc.getDocumentElement().normalize();

            System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
            NodeList nList = doc.getElementsByTagName("Customers");

            for (int temp = 0; temp < nList.getLength(); temp++) {
                Node nNode = nList.item(temp);
                System.out.println("\nCurrent Element :" + nNode);
                if (nNode.getNodeType() == Node.ELEMENT_NODE) {

                    Element eElement = (Element) nNode;


                    System.out.println("Customer id : "
                            + ((Element) eElement.getElementsByTagName("Customer").item(0)).getAttribute("Name"));
                    System.out.println("IataCode : "
                            + ((Element) eElement.getElementsByTagName("Customer").item(0)).getAttribute("Code"));
                    System.out.println("IcaoCode : "
                            + ((Element) eElement.getElementsByTagName("Customer").item(0)).getAttribute("Purpose"));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

But I am not getting the values of second record. which are details of customer "Test_92".

Can someone help me to read the values of N number of customers?

Thanks in Advance!


You are looping over the worn thing.

You get your list of elements called "customers" (you only have one of these) then loop over it.

Then inside your loop you find the list of "customer" take the first entry then extract an attribute (three times).

What you need to do is, take the first entry of your "customers" list, then get the list of "customer" and) loop over that.

How to read multiple inner tag in xml in Java (Java in General forum , Post by: Dinesh Pise , Ranch Hand. Jul 28, 2012 00:04:40. [size=12]Dear Friends​, Please help me in reading multiple inner xml tags in java. provide a code as how to read PANNO I get values of USERID and application but only one value first PANNO and then Can you please tell us what line 61 of your source file is? Home » Java » Read XML file with more than one value in one tag Using Java Read XML file with more than one value in one tag Using Java Posted by: admin July 12, 2018 Leave a comment


Try to use xPath.

You can easily jump from node to node with it.

XML:

<Customers>
   <Customer name='f1'/>
   <Customer name='f1'/>
   <Customer name='f1'/>
</Customers>

Java:

Document xmlDocument = builder.parse(new FileInputStream("Customers.xml"));

XPath xPath =  XPathFactory.newInstance().newXPath();

NodeList nodeList = (NodeList) xPath.compile("/Customers/Customer").evaluate(xmlDocument,  XPathConstants.NODESET);

for (int i = 0; i < nodeList.getLength(); i++)
{
    System.out.println(nodeList.item(i).getAttributes().getNamedItem("name").getNodeValue()); 
}

Outpot:

f1
f2
f3

Java Read XML - Java DOM Parser Example, In this tutorial, learn to read or parse XML document using Java DOM DOM parser is intended for working with XML as an object graph (a tree like Read More : Difference between DOM parser and SAX parser getAttributes(); //​returns a Map (table) of names/values You are not using [xml] your XML here [/​xml] tags. There are better implementations of reading XML files which would work for any XML file. The above one would require a few changes every time the XML tag names change. But this is much more simpler than the other programs.


You are getting the list of Customers tag, which is only in you document.

Try this,

 NodeList nList = doc.getElementsByTagName("Customer");

            for (int temp = 0; temp < nList.getLength(); temp++) {
                Node nNode = nList.item(temp);
                System.out.println("\nCurrent Element :" + nNode);
                if (nNode.getNodeType() == Node.ELEMENT_NODE) {

                    Element eElement = (Element) nNode;


                    System.out.println("Customer id : "
                            + ( eElement.getAttribute("Name")));
                    System.out.println("IataCode : "
                            + ( eElement.getAttribute("Code")));
                    System.out.println("IcaoCode : "
                            + ( eElement.getAttribute("Purpose")));

Parsing xml with multiple child nodes in java, We can read an XML file in several ways depends on our requirement. Merge multiple nodes of multiple XML files using JAVA I have some xml files in an where I have multiple nodes in a tag and I need to extract the data using extractvalue funtion. Get each xml child element value by passing element node name. How to get the value of a tag in xml using java . is more proficient but looks more readable. please use code tag henceforth. to delete xml files after reading


SimpleXml can do it:

final String data = ...
final SimpleXml simple = new SimpleXml();
final Element customers = simple.fromXml(data);
for (final Element customer : customers.children) {
    System.out.println(customer.attributes.get("Name"));
}

Will output:

Test_91
Test_92

From maven central:

<dependency>
    <groupId>com.github.codemonstur</groupId>
    <artifactId>simplexml</artifactId>
    <version>1.4.0</version>
</dependency>

Improve your code with lint checks, To learn more about running Gradle tasks from the command line, read Build Your App from The lint.xml file consists of an enclosing <lint> parent tag that contains one or more children Lint defines a unique id attribute value for each <issue> . You can disable lint from checking your Java, Kotlin, and XML source files. In this Java xml parser tutorial, Learn to read xml with DOM parser in Java. DOM parser is intended for working with XML as an object graph (a tree like structure) in memory – so called “ Document Object Model (DOM) “. In first, the parser traverses the input XML file and creates DOM objects corresponding to the nodes in XML file.


XML and XPath, XPath is a syntax for defining parts of an XML document; XPath uses path expressions to JavaScript, Java, XML Schema, PHP, Python, C and C++, and lots of other languages. We will use the following XML document: of the bookstore element that have a price element with a value greater than 35.00 Read More »  XML elements can have attributes, just like HTML. Attributes are designed to contain data related to a specific element. Attribute values must always be quoted. Either single or double quotes can be used. If the attribute value itself contains double quotes you can use single quotes, like in this example: In the first example gender is an


XQuery Selecting and Filtering, We will use the "books.xml" document in the examples below (same XML file as in the It is also allowed with more than one in expression in the for clause. Use​  Suppose you have an XML file and you just want to read and display the whole file as String in Java, may be for debugging purpose. If you are wondering how to do that in Java, well there are many ways to read XML as String in Java. You can do it in one line if you are fine with using an open source library or you can do it in a couple of lines


How to get child nodes of a node in xml using java, Dec 06, 2008 · In this tutorial, we will show you how to read an XML file via DOM I use the Get Multiple Nodes command and give the path to block of child notes the values of some tags, as such: Java XPath Parser - Parse XML Document  How to read multiple inner tag in xml in Java . Dinesh Pise get values of USERID and application but only one value first PANNO and then null pointer exception