I have this XML file.I just parse this XML file.This example shows how to get the node by “name”, and display the value.How to show all records from database?
<data399173_eff_sor>
<record>
<ID>1</ID>
<item_no>1.0</item_no>
<description>Hack off tiles and make good walls</description>
<price>100</price>
<base_qty>50</base_qty>
<var_qty>20</var_qty>
<base_price_>5000</base_price_>
</record>
<record>
<ID>1</ID>
<item_no>1.03</item_no>
<description>Test</description>
<price>45</price>
<base_qty>100</base_qty>
<var_qty>4500</var_qty>
<base_price_>0</base_price_>
</record>
</data399173_eff_sor>
and so on
Java code
File fXmlFile = new File("D:/formdata.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("record");
System.out.println("----------------------------");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
System.out.println("\nCurrent Element :" + nNode.getNodeName());
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
System.out.println("Item No : " + eElement.getElementsByTagName("item_no").item(0).getTextContent());
System.out.println("Description : " + eElement.getElementsByTagName("description").item(0).getTextContent());
System.out.println("price : " + eElement.getElementsByTagName("price").item(0).getTextContent());
System.out.println("base qty : " + eElement.getElementsByTagName("base_qty").item(0).getTextContent());
System.out.println("Var qty : " + eElement.getElementsByTagName("var_qty").item(0).getTextContent());
System.out.println("Base price : " + eElement.getElementsByTagName("base_price_").item(0).getTextContent());
}
In this its just show first record.i want to display all records in the data base
An XML parser is a software library or package that provides interfaces for client applications to work with an XML document. The XML Parser is designed to read the XML and create a way for programs to use XML. XML parser validates the document and check that the document is well formatted.
DOM is an acronym stands for Document Object Model. It defines a standard way to access and manipulate documents. The Document Object Model (DOM) is a programming API for HTML and XML documents. It defines the logical structure of documents and the way a document is accessed and manipulated.
DOM parser parses the entire XML file and creates a DOM object in the memory. It models an XML file in a tree structure for easy traversal and manipulation. In DOM everything in an XML file is a node. The node represents a component of an XML file.
The XML Document Object Model (DOM) class is an in-memory representation of an XML document. The DOM allows you to programmatically read, manipulate, and modify an XML document. The XmlReader class also reads XML; however, it provides non-cached, forward-only, read-only access.
The xml is not valid. (You can validate your xml online: http://www.w3schools.com/xml/xml_validator.asp)
You can try with this xml
<records>
<record>
<ID>1</ID>
<item_no>1.0</item_no>
<description>Hack off tiles and make good walls</description>
<price>100</price>
<base_qty>50</base_qty>
<var_qty>20</var_qty>
<base_price_>5000</base_price_>
</record>
<record>
<ID>1</ID>
<item_no>1.03</item_no>
<description>Test</description>
<price>45</price>
<base_qty>100</base_qty>
<var_qty>4500</var_qty>
<base_price_>0</base_price_>
</record>
</records>
and keep your code
package test;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.IOException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class TestXml{
public static void main (String[] args) throws ParserConfigurationException{
TestXml t = new TestXml();
t.readXml() ;
}
public void readXml () throws ParserConfigurationException{
File fXmlFile = new File("D:/formdata.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = null;
try {
doc = dBuilder.parse(fXmlFile);
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
doc.getDocumentElement().normalize();
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("record");
System.out.println("----------------------------");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
System.out.println("\nCurrent Element :" + nNode.getNodeName());
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
System.out.println("Item No : " + eElement.getElementsByTagName("item_no").item(0).getTextContent());
System.out.println("Description : " + eElement.getElementsByTagName("description").item(0).getTextContent());
System.out.println("price : " + eElement.getElementsByTagName("price").item(0).getTextContent());
System.out.println("base qty : " + eElement.getElementsByTagName("base_qty").item(0).getTextContent());
System.out.println("Var qty : " + eElement.getElementsByTagName("var_qty").item(0).getTextContent());
System.out.println("Base price : " + eElement.getElementsByTagName("base_price_").item(0).getTextContent());
}}}}
and you will have this result
Root element :records
----------------------------
Current Element :record
Item No : 1.0
Description : Hack off tiles and make good walls
price : 100
base qty : 50
Var qty : 20
Base price : 5000
Current Element :record
Item No : 1.03
Description : Test
price : 45
base qty : 100
Var qty : 4500
Base price : 0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With