We need to read XML file for Automation,when some testdata is into the xml file.There are many way to read xml file and many API(Application Programming Interface) for this. We will try to learn here
1.DOM (Document Object Model)
2.SAX (Simple API for XML)
3.STAX (STreaming API for XML)
a. Event or Iterator API
b. Stream or Cursor API
Here we learn DOM parser later we know how to use SAX and STAX parser.
To reading any XML file using DOM we need to follow this steps:
Step 1:
Create DocumentBuilderFactory using
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance()
Step 2:
Create DocumentBuilder object
DocumentBuilder db = dbf.newDocumentBuilder();
Step 3:
Create Document from file.
Document doc = db.parse(new File("filemane"));
Step 4:
Now Parse the the XML document
package domparsing;
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class DOMParsing {
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
/**
* DocumentBuilderFactory.newInstance() return instance of XMLDom parser
* We can also specify the ClassName directly for XMLParser in newInstance method like
* DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance
* ("com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl",null)
*/
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new File("c:/test.xml"));
doc.normalizeDocument();
/**
* Getting Particular node details
*/
getNodeDetails(doc, "source");
if (doc.hasChildNodes()) {
/**
* Getting all child node from XMLDocument using
* doc.getDocumentElement().getChildNodes() Or doc.getChildNodes()
*/
//getAllNodeNAttribute(doc.getDocumentElement().getChildNodes());
getAllNodeNAttribute(doc.getChildNodes());
}
}
public static void getAllNodeNAttribute(NodeList nodes) {
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
System.out.println("Node Start: " + node.getNodeName() + " [Start]");
if (node.hasAttributes()) {
// get attributes names and values
NamedNodeMap nodeAttrbs = node.getAttributes();
for (int j = 0; j < nodeAttrbs.getLength(); j++) {
Node attrNode = nodeAttrbs.item(j);
System.out.println("attr name: " + attrNode.getNodeName());
System.out.println("attr value: " + attrNode.getNodeValue());
}
}
if (node.hasChildNodes()) {
getAllNodeNAttribute(node.getChildNodes());
System.out.println("Node Close: " + node.getNodeName() + " [CLOSE]");
}
if (!node.hasChildNodes()) {
System.out.println("Node Close: " + node.getNodeName() + " [CLOSE]");
}
} else if ((nodes.getLength() <= 1)) {
System.out.println("Node Text: " + node.getNodeValue());
}
}
}
public static void getNodeDetails(Document doc, String nodeName) {
NodeList nodes = doc.getElementsByTagName(nodeName);
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
System.out.println("Node Name : " + node.getNodeName());
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node;
System.out.println("Type Attribute : " + element.getAttribute("type"));
System.out.println("Media Attribute : " + element.getAttribute("media"));
if (element.getElementsByTagName("special").getLength() > 0) {
System.out.println("Special Tag : " + element.getElementsByTagName("special").item(0).getTextContent());
System.out.println("Special Tag Attribue : " + element.getElementsByTagName("special").item(0).getAttributes().item(0).getNodeName());
}
}
}
}
}
Example XML:
<?xml version="1.0" encoding="UTF-8"?>
<ep>
<source type="xml">TEST</source>
<source media="text">
<special type="xml">Special Text</special>
</source>
</ep>
1.DOM (Document Object Model)
2.SAX (Simple API for XML)
3.STAX (STreaming API for XML)
a. Event or Iterator API
b. Stream or Cursor API
Here we learn DOM parser later we know how to use SAX and STAX parser.
To reading any XML file using DOM we need to follow this steps:
Step 1:
Create DocumentBuilderFactory using
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance()
Step 2:
Create DocumentBuilder object
DocumentBuilder db = dbf.newDocumentBuilder();
Step 3:
Create Document from file.
Document doc = db.parse(new File("filemane"));
Step 4:
Now Parse the the XML document
package domparsing;
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class DOMParsing {
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
/**
* DocumentBuilderFactory.newInstance() return instance of XMLDom parser
* We can also specify the ClassName directly for XMLParser in newInstance method like
* DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance
* ("com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl",null)
*/
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new File("c:/test.xml"));
doc.normalizeDocument();
/**
* Getting Particular node details
*/
getNodeDetails(doc, "source");
if (doc.hasChildNodes()) {
/**
* Getting all child node from XMLDocument using
* doc.getDocumentElement().getChildNodes() Or doc.getChildNodes()
*/
//getAllNodeNAttribute(doc.getDocumentElement().getChildNodes());
getAllNodeNAttribute(doc.getChildNodes());
}
}
public static void getAllNodeNAttribute(NodeList nodes) {
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
System.out.println("Node Start: " + node.getNodeName() + " [Start]");
if (node.hasAttributes()) {
// get attributes names and values
NamedNodeMap nodeAttrbs = node.getAttributes();
for (int j = 0; j < nodeAttrbs.getLength(); j++) {
Node attrNode = nodeAttrbs.item(j);
System.out.println("attr name: " + attrNode.getNodeName());
System.out.println("attr value: " + attrNode.getNodeValue());
}
}
if (node.hasChildNodes()) {
getAllNodeNAttribute(node.getChildNodes());
System.out.println("Node Close: " + node.getNodeName() + " [CLOSE]");
}
if (!node.hasChildNodes()) {
System.out.println("Node Close: " + node.getNodeName() + " [CLOSE]");
}
} else if ((nodes.getLength() <= 1)) {
System.out.println("Node Text: " + node.getNodeValue());
}
}
}
public static void getNodeDetails(Document doc, String nodeName) {
NodeList nodes = doc.getElementsByTagName(nodeName);
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
System.out.println("Node Name : " + node.getNodeName());
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node;
System.out.println("Type Attribute : " + element.getAttribute("type"));
System.out.println("Media Attribute : " + element.getAttribute("media"));
if (element.getElementsByTagName("special").getLength() > 0) {
System.out.println("Special Tag : " + element.getElementsByTagName("special").item(0).getTextContent());
System.out.println("Special Tag Attribue : " + element.getElementsByTagName("special").item(0).getAttributes().item(0).getNodeName());
}
}
}
}
}
Example XML:
<?xml version="1.0" encoding="UTF-8"?>
<ep>
<source type="xml">TEST</source>
<source media="text">
<special type="xml">Special Text</special>
</source>
</ep>
No comments:
Post a Comment