Accessing an XML document to retrieve element values
Requirement:
We have an XML file with couple of elements wrapped in a parent element. We need to parse the XML file and retrieve only the child element values.
XML File (ABCSQLQueries):
<ABCQUERIES>
<TBUUSLIST_QUERY>
SELECT TBUISUSKEY,TBUISUSNAME FROM OWN_ABC.TBUISUSLABELS
</TBUUSLIST_QUERY>
<AVAILABLEWITH_QUERY>
SELECT A.REQUESTAVAILABLEKEY, A.DISPLAY FROM OWN_ ABC.REQUESTAVAILABLE A, OWN_ ABC.REQUESTDETAILS B WHERE B.REQUESTDETAILSKEY = ? AND A.REQUESTAVAILABLEKEY = B.REQUESTAVAILABLEKEY
</AVAILABLEWITH_QUERY>
</ABCQUERIES>
Code:
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.Attr;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.DOMException;
import org.xml.sax.SAXException;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
public class XmlReading {
Document doc;
Element element;
private static String fileName = "ABCSQLQueries.xml";
public static void main(String[] args) {
XmlReading xr = new XmlReading();
xr.getXmlParser();
// xr.getXmlParser(args);
}
public void getXmlParser() {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder builder = factory.newDocumentBuilder();
InputStream objInputStream = this.getClass().getResourceAsStream(fileName);
doc = builder.parse(objInputStream);
}catch(ParserConfigurationException parserException) {
}catch(SAXException saxException) {
}catch(IOException ioException) {
}catch(Exception exception){}
getAttributes();
}
public void getXmlParser(String[] args) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
if(args.length != 1) {
System.err.println("XML File Required");
}
try {
DocumentBuilder builder = factory.newDocumentBuilder();
doc = builder.parse(new File(args[0]));
}catch(ParserConfigurationException e1) {
}catch(SAXException e2) {
}catch(IOException e3) {
}
getAttributes();
}
public void getAttributes() {
// Retrive the entire Document from the Dom Tree
element = doc.getDocumentElement();
String s1 = element.getTagName();
System.out.println("\nRoot Tag Name: " + s1);
// To get all the elements in a DOM Tree
NodeList nl1 = element.getElementsByTagName("*");
int i2 = nl1.getLength();
System.out.println("No of Elements: " + i2 + "\n");
for(int i=0; i<i2; i++) {
Node node = nl1.item(i);
System.out.println("Node Name: " + node.getNodeName());
System.out.println("Node Value: " + node.getFirstChild().getNodeValue());
}
}
}
XML parsing has been done in both ways like user can give the XML file as input through console as well as hard-code the XML file name directly in the parsing java file.
We have an XML file with couple of elements wrapped in a parent element. We need to parse the XML file and retrieve only the child element values.
XML File (ABCSQLQueries):
<ABCQUERIES>
<TBUUSLIST_QUERY>
SELECT TBUISUSKEY,TBUISUSNAME FROM OWN_ABC.TBUISUSLABELS
</TBUUSLIST_QUERY>
<AVAILABLEWITH_QUERY>
SELECT A.REQUESTAVAILABLEKEY, A.DISPLAY FROM OWN_ ABC.REQUESTAVAILABLE A, OWN_ ABC.REQUESTDETAILS B WHERE B.REQUESTDETAILSKEY = ? AND A.REQUESTAVAILABLEKEY = B.REQUESTAVAILABLEKEY
</AVAILABLEWITH_QUERY>
</ABCQUERIES>
Code:
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.Attr;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.DOMException;
import org.xml.sax.SAXException;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
public class XmlReading {
Document doc;
Element element;
private static String fileName = "ABCSQLQueries.xml";
public static void main(String[] args) {
XmlReading xr = new XmlReading();
xr.getXmlParser();
// xr.getXmlParser(args);
}
public void getXmlParser() {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder builder = factory.newDocumentBuilder();
InputStream objInputStream = this.getClass().getResourceAsStream(fileName);
doc = builder.parse(objInputStream);
}catch(ParserConfigurationException parserException) {
}catch(SAXException saxException) {
}catch(IOException ioException) {
}catch(Exception exception){}
getAttributes();
}
public void getXmlParser(String[] args) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
if(args.length != 1) {
System.err.println("XML File Required");
}
try {
DocumentBuilder builder = factory.newDocumentBuilder();
doc = builder.parse(new File(args[0]));
}catch(ParserConfigurationException e1) {
}catch(SAXException e2) {
}catch(IOException e3) {
}
getAttributes();
}
public void getAttributes() {
// Retrive the entire Document from the Dom Tree
element = doc.getDocumentElement();
String s1 = element.getTagName();
System.out.println("\nRoot Tag Name: " + s1);
// To get all the elements in a DOM Tree
NodeList nl1 = element.getElementsByTagName("*");
int i2 = nl1.getLength();
System.out.println("No of Elements: " + i2 + "\n");
for(int i=0; i<i2; i++) {
Node node = nl1.item(i);
System.out.println("Node Name: " + node.getNodeName());
System.out.println("Node Value: " + node.getFirstChild().getNodeValue());
}
}
}
XML parsing has been done in both ways like user can give the XML file as input through console as well as hard-code the XML file name directly in the parsing java file.
0 Comments:
Post a Comment
<< Home