Saturday, May 18, 2024
HomeJavaTips on how to Parse or Learn XML File in Java >>...

Tips on how to Parse or Learn XML File in Java >> XML Tutorial Instance


Java gives in depth help for studying XML file, writing XML file and accessing any component from XML file. All XML parsing associated lessons and strategies are inside JAXP. Although DOM associated code comes from org.w3c.dom package deal. All XML parsers are in javax.xml.parsers package deal.we’ll see instance of parsing xml recordsdata utilizing JAXP API in subsequent part.

Right here is xml file Shares.xml which incorporates some shares and there value, amount we’ll use this in our xml parsing instance in Java.

<?xml model=“1.0” encoding=“UTF-8”?>

<shares>

       <inventory>

              <image>Citibank</image>

              <value>100</value>

              <amount>1000</amount>

       </inventory>

       <inventory>

              <image>Axis financial institution</image>

              <value>90</value>

              <amount>2000</amount>

       </inventory>

</shares>

3. Code Instance of Parsing XML File in Java utilizing DOM Parser

import java.io.File;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Doc;

import org.w3c.dom.Aspect;

import org.w3c.dom.Node;

import org.w3c.dom.NodeList;

public class DOMExampleJava {

public static void important(String args[]) {

strive {

File shares = new File(“Shares.xml”);

DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();

DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();

Doc doc = dBuilder.parse(shares);

doc.getDocumentElement().normalize();

System.out.println(“root of xml file” + doc.getDocumentElement().getNodeName());

NodeList nodes = doc.getElementsByTagName(“inventory”);

System.out.println(“==========================”);

for (int i = 0; i < nodes.getLength(); i++) {

Node node = nodes.merchandise(i);

if (node.getNodeType() == Node.ELEMENT_NODE) {

Aspect component = (Aspect) node;

System.out.println(“Inventory Image: “ + getValue(“image”, component));

System.out.println(“Inventory Value: “ + getValue(“value”, component));

System.out.println(“Inventory Amount: “ + getValue(“amount”, component));

}

}

} catch (Exception ex) {

ex.printStackTrace();

}

}

non-public static String getValue(String tag, Aspect component) {

NodeList nodes = component.getElementsByTagName(tag).merchandise(0).getChildNodes();

Node node = (Node) nodes.merchandise(0);

return node.getNodeValue();

}

}

Output:

root of xml file shares

==========================

Inventory Image: Citibank

Inventory Value: 100

Inventory Amount: 1000

Inventory Image: Axis financial institution

Inventory Value: 90

Inventory Amount: 2000

That’s all on xml parsing in java for now. Now we have seen tips on how to learn and write xml recordsdata in Java and are actually acquainted with each DOM and SAX Parser in java. We are going to see extra on xml within the coming days like studying xml components by way of XPath and utilizing xml beans and so forth. let me know when you’ve got any doubt on xml parsing examples or normally with xml and Java.



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments