cancel
Showing results for 
Search instead for 
Did you mean: 

Read Data from the XML File and fill the context node attributes

Former Member
0 Kudos

Hello Everybody,

I have a XML file which has the following structure.

<?xml version="1.0"?>

<mydata>

<data>

<key>1</key>

<name>A</name>

<id>123</id>

</data>

<data>

<key>2</key>

<name>B</name>

<id>456</id>

</data>

</mydata>

I have a similar node structure in my Controller context with mydata as the root node and data as the child node and having attributes as key, name, id.

I want to read the XML file and fill the context with the values present in the XML file. I am able to parse the XML file using document builder class but when I am accessing the data node in the XML file I want to get the childnodes automatically . Plz help me how to get the nodes automatically so tthat I can fill the context attributes using for loop.

Accepted Solutions (0)

Answers (2)

Answers (2)

Former Member
0 Kudos

Hi,

No there is no such Apis available in which you can directly get the Values between the tags.

You can make the process fast by using SAX parser if you have to parse large xml file but still you need to specify the tags.

Regards

Narendra Singh

Former Member
0 Kudos

Hi,

Below is the code to read sample xml as given by you.

In this i have written System.out.println to print the values of key, name,id but you can directly assign the values to the node element.

i hope this will help . please revert if you require more info

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;

import org.w3c.dom.Element;

import org.w3c.dom.Node;

import org.w3c.dom.NodeList;

public class XMLReader {

public static void main(String argv[]) {

try {

File file = new File("d:
data.xml");

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

DocumentBuilder db = dbf.newDocumentBuilder();

Document doc = db.parse(file);

doc.getDocumentElement().normalize();

System.out.println("Root element " + doc.getDocumentElement().getNodeName());

NodeList nodeLst = doc.getElementsByTagName("data");

for (int s = 0; s < nodeLst.getLength(); s++) {

Node fstNode = nodeLst.item(s);

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

Element fstElmnt = (Element) fstNode;

NodeList fstNmElmntLst = fstElmnt.getElementsByTagName("key");

Element fstNmElmnt = (Element) fstNmElmntLst.item(0);

NodeList fstNm = fstNmElmnt.getChildNodes();

System.out.println("Key : " + ((Node) fstNm.item(0)).getNodeValue());

NodeList lstNmElmntLst = fstElmnt.getElementsByTagName("name");

Element lstNmElmnt = (Element) lstNmElmntLst.item(0);

NodeList lstNm = lstNmElmnt.getChildNodes();

System.out.println("Name : " + ((Node) lstNm.item(0)).getNodeValue());

NodeList idElmntLst = fstElmnt.getElementsByTagName("id");

Element idElmnt = (Element) idElmntLst.item(0);

NodeList id = idElmnt.getChildNodes();

System.out.println("Id: " + ((Node) id.item(0)).getNodeValue());

}

}

} catch (Exception e) {

e.printStackTrace();

}

}

}

Former Member
0 Kudos

Hi Narendra,

I was also looking for such a XML reading. I found a similar method at this [link|http://www.developerfusion.com/code/2064/a-simple-way-to-read-an-xml-file-in-java/].

Is there not a direct way or some APIs that can work in a generic way for any Context Nodes??

Dont you think that would be more usefull !!

If i were to change my structure of Node/XML or rename it, I will have to revisit the code to make new changes everytime.

-kunal kotak