Accessing an XML document to retrieve attribute values
Requirement:
We have an XML file with a element and couple of attributes in it. We need to parse the XML file and retrieve only the attribute values.
XML File (ABCSQLQueries):
<ABCQUERIES
TBUUSLIST_QUERY="SELECT TBUISUSKEY,TBUISUSNAME FROM OWN_ABC.TBUISUSLABELS"
AVAILABLEWITH_QUERY="SELECT A.REQUESTAVAILABLEKEY, A.DISPLAY FROM OWN_ABC.REQUESTAVAILABLE A, OWN_ABC.REQUESTDETAILS
B WHERE B.REQUESTDETAILSKEY = ? AND A.REQUESTAVAILABLEKEY = B.REQUESTAVAILABLEKEY"
/>
Code:
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;
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 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();
// Retrive the attribute list from document
NamedNodeMap attrs = element.getAttributes();
// Get number of attributes in the element
int numAttrs = attrs.getLength();
System.out.println("\n Number of Attribute: " + numAttrs + "\n");
// Process each attribute
for (int i=0; i<numAttrs; i++) {
Node node = attrs.item(i);
// Get attribute name and value
String attrName = node.getNodeName();
String attrValue = node.getNodeValue();
System.out.println("Attribute Name: " + attrName + "\nAttribute Value: " + attrValue + "\n");
}
}
}
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 a element and couple of attributes in it. We need to parse the XML file and retrieve only the attribute values.
XML File (ABCSQLQueries):
<ABCQUERIES
TBUUSLIST_QUERY="SELECT TBUISUSKEY,TBUISUSNAME FROM OWN_ABC.TBUISUSLABELS"
AVAILABLEWITH_QUERY="SELECT A.REQUESTAVAILABLEKEY, A.DISPLAY FROM OWN_ABC.REQUESTAVAILABLE A, OWN_ABC.REQUESTDETAILS
B WHERE B.REQUESTDETAILSKEY = ? AND A.REQUESTAVAILABLEKEY = B.REQUESTAVAILABLEKEY"
/>
Code:
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;
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 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();
// Retrive the attribute list from document
NamedNodeMap attrs = element.getAttributes();
// Get number of attributes in the element
int numAttrs = attrs.getLength();
System.out.println("\n Number of Attribute: " + numAttrs + "\n");
// Process each attribute
for (int i=0; i<numAttrs; i++) {
Node node = attrs.item(i);
// Get attribute name and value
String attrName = node.getNodeName();
String attrValue = node.getNodeValue();
System.out.println("Attribute Name: " + attrName + "\nAttribute Value: " + attrValue + "\n");
}
}
}
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