Vakils
asked on
XML parsing in Java
How to parse XML below in Java using DOM Parser:
<employees>
<employee id="111">
<firstName>Rakesh</firstNa me>
<lastName>Mishra</lastName >
<location>Bangalore</locat ion>
</employee>
<employee id="112">
<firstName>John</firstName >
<lastName>Davis</lastName>
<location>Chennai</locatio n>
</employee>
<employee id="113">
<firstName>Rajesh</firstNa me>
<lastName>Sharma</lastName >
<location>Pune</location>
</employee>
</employees>
<employees>
<employee id="111">
<firstName>Rakesh</firstNa
<lastName>Mishra</lastName
<location>Bangalore</locat
</employee>
<employee id="112">
<firstName>John</firstName
<lastName>Davis</lastName>
<location>Chennai</locatio
</employee>
<employee id="113">
<firstName>Rajesh</firstNa
<lastName>Sharma</lastName
<location>Pune</location>
</employee>
</employees>
ASKER
XML comes from file. Reading nodes value, is what I want. XML elements have no attributes at this stage and some elements may not any have value. For example while traversing XML tree, if an element has no value, I get null pointer exception in element.getTextContent(). Is there a elegant way to know that element has no value, rather than checking if element is null?
XML:
<contracts>
<contract>123</contract> // OK
<contract_year></contract_ year> //exception
</contracts>
//Iterating through the nodes and extracting the data.
NodeList nodeList = document.getDocumentElemen t().getChi ldNodes();
for (int i = 0; i < nodeList.getLength(); i++) {
//We have encountered an <employee> tag.
Node node = nodeList.item(i);
if (node instanceof Element) {
Employee emp = new Employee();
emp.id = node.getAttributes().
getNamedItem("id").getNode Value();
NodeList childNodes = node.getChildNodes();
for (int j = 0; j < childNodes.getLength(); j++) {
Node cNode = childNodes.item(j);
//Identifying the child tag of employee encountered.
if (cNode instanceof Element) {
String content = cNode.getLastChild().getTe xtContent( ).trim(); // null pointer exception if element has no value
XML:
<contracts>
<contract>123</contract> // OK
<contract_year></contract_
</contracts>
//Iterating through the nodes and extracting the data.
NodeList nodeList = document.getDocumentElemen
for (int i = 0; i < nodeList.getLength(); i++) {
//We have encountered an <employee> tag.
Node node = nodeList.item(i);
if (node instanceof Element) {
Employee emp = new Employee();
emp.id = node.getAttributes().
getNamedItem("id").getNode
NodeList childNodes = node.getChildNodes();
for (int j = 0; j < childNodes.getLength(); j++) {
Node cNode = childNodes.item(j);
//Identifying the child tag of employee encountered.
if (cNode instanceof Element) {
String content = cNode.getLastChild().getTe
ASKER
XML:
<employee>
<Name>SAM</Name> // OK
<Middle></Middle> //exception
</employee>
//Iterating through the nodes and extracting the data.
NodeList nodeList = document.getDocumentElemen t().getChi ldNodes();
for (int i = 0; i < nodeList.getLength(); i++) {
//We have encountered an <employee> tag.
Node node = nodeList.item(i);
if (node instanceof Element) {
Employee emp = new Employee();
emp.id = node.getAttributes().
getNamedItem("id").getNode Value();
NodeList childNodes = node.getChildNodes();
for (int j = 0; j < childNodes.getLength(); j++) {
Node cNode = childNodes.item(j);
//Identifying the child tag of employee encountered.
if (cNode instanceof Element) {
String content = cNode.getLastChild().getTe xtContent( ).trim(); // null pointer exception if element has no value
<employee>
<Name>SAM</Name> // OK
<Middle></Middle> //exception
</employee>
//Iterating through the nodes and extracting the data.
NodeList nodeList = document.getDocumentElemen
for (int i = 0; i < nodeList.getLength(); i++) {
//We have encountered an <employee> tag.
Node node = nodeList.item(i);
if (node instanceof Element) {
Employee emp = new Employee();
emp.id = node.getAttributes().
getNamedItem("id").getNode
NodeList childNodes = node.getChildNodes();
for (int j = 0; j < childNodes.getLength(); j++) {
Node cNode = childNodes.item(j);
//Identifying the child tag of employee encountered.
if (cNode instanceof Element) {
String content = cNode.getLastChild().getTe
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
OK. Got it. Calling trim is OK. getLastChild is not needed.
Thanks!
Thanks!
Not a problem, glad to help! :)
Open in new window