Reading XML using Java

I want to read all <temp >nodes map them to some variable and create one more XML.

I am stuck up in the first place itself where i need to read the data :( . I am getting null pointer exception, please find the code also.

<?xml version="1.0" encoding="UTF-8"?>

<ftpl id="47" name="XXXXXXXX xxx xxxxxxxx">

<abc-cab name="Systems" id="43434">

<abc-dr name="zzzzz" id="4545454">

<temp key="Type">null</param>

<temp key="Folder">/ZVZ/XXXXX - xyz</param>

<temp key="Author">XYZ,ABC</param>

<temp key="Comments">null</param>

<temp key="Mailed_By">XYZ,ABC</param>

<temp key="Subject">null</param>

<temp key="Key_Words">null</param>

<abc-page name="1" id="3524012">

<file text="/asas/asasa/asa/asas/Oxxxxxx2276744.txt" count="1"/>

</abc-page>

</abc-dr>

</abc-cab>

</ftpl>

try {

File file = new File("source-file");

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("abc-dr");

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("abc-dr");

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

NodeList fstNm = fstNmElmnt.getChildNodes();

System.out.println("First Node : " + ((Node) fstNm.item(1)).getNodeValue());

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

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

NodeList lstNm = lstNmElmnt.getChildNodes();

}

}

} catch (Exception e) {

e.printStackTrace();

}

Please help.. let me know the code which read XML I have tried with all SAX/DOM i am getting same NUllPointerExcetion

Message was edited by:

JavaNewUser

[2348 byte] By [JavaNewUsera] at [2007-11-27 1:23:26]
# 1

At a guess I would say it was a problem with either your File code or your Elements.

To help us diagnose your problem could you please:

1. Post the exact null pointer message you're getting.

2. Re-post your code, but highlight it and press the 'Code' button, this makes it far easier to read.

patricknza at 2007-7-12 0:13:02 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2

Hi

Thanks patricknz

pls find the XMl and the java code

I have again modified the code .. but got the same result as below

[temp: null]-param =

[temp: null]-param =

[temp: null]-param =

[temp: null]-param =

[temp: null]-param =

[temp: null]-param =

[temp: null]-param =

[abc-page: null]-abc-page=

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;

import org.w3c.dom.Node;

import org.w3c.dom.NodeList;

import org.w3c.dom.Element;

import java.lang.*;

public class SampleDOM

{

public static void main(String s[]){

try{

DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();

Document document = docBuilder.parse("C:\\demo.xml");

NodeList nodeList = document.getElementsByTagName("abc-dr");

for (int i = 0, length1 = nodeList.getLength(); i < length1; i++) {

final Element rDoc = (Element)nodeList.item(i);

final NodeList rDocC = rDoc.getChildNodes();

for (int j = 0, length2 = rDocC.getLength(); j < length2; j++) {

final Node child = rDocC.item(j);

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

Element element = (Element)child;

System.out.println(element.getChildNodes() + "-" +

new StringBuffer(element.getNodeName()).append(" = "));

}

}

}

}catch(Exception e){

e.printStackTrace();

}

}

}

XML FIle:-

<?xml version="1.0" encoding="UTF-8"?>

<ftpl id="47" name="XXXXXXXX xxx xxxxxxxx">

<abc-cab name="Systems" id="43434">

<abc-dr name="zzzzz" id="4545454">

<temp key="Type">null</param>

<temp key="Folder">/ZVZ/XXXXX - xyz</param>

<temp key="Author">XYZ,ABC</param>

<temp key="Comments">null</param>

<temp key="Mailed_By">XYZ,ABC</param>

<temp key="Subject">null</param>

<temp key="Key_Words">null</param>

<abc-page name="1" id="3524012">

<file text="/asas/asasa/asa/asas/Oxxxxxx2276744.txt" count="1"/>

</abc-page>

</abc-dr>

</abc-cab>

</ftpl>

Message was edited by:

JavaNewUser

Message was edited by:

JavaNewUser

JavaNewUsera at 2007-7-12 0:13:02 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 3
The stack trace will tell you the exact line of code that throws the exception. That information is critical for you to solve the problem, so take note of it when you next run the code. And tell us which line it is, if you still can't solve it.
DrClapa at 2007-7-12 0:13:02 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 4
I'm sorry, are you still getting the exception or is it outputting as you've shown?It looks like your code is working, though I'm not sure that the output is what you're expecting.What exactly do you want to get out of the XML document?
patricknza at 2007-7-12 0:13:02 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 5

It looks to me like you are making your variables into constants, which means they won't be changed, so rather than

for (int j = 0, length2 = rDocC.getLength(); j < length2; j++) {

final Node child = rDocC.item(j);

// ...

}

i think you should try

Node child = null;

for (int j = 0, length2 = rDocC.getLength(); j < length2; j++) {

child = rDocC.item(j);

// ...

}

and the same for all the other variables that you make final.

ethical_anarhista at 2007-7-12 0:13:02 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 6

HI All,

Thanks for the reply i got the answer I have to use

element.getTextContent()

to fetch the values .. Sorry for the late reply.. Now the output what iam getting have to store in the variables and with the help of those variables i have to generate New XML file..

Now the questions are:-

1. Is it right to write Getter/Setter class to store the output into variables?

2. How to create XML using java? (With Getter/Setter Class)

One More Problem i have just noticed :-

Not able to read

<abc-page name="1" id="3524012">

<file text="/asas/asasa/asa/asas/Oxxxxxx2276744.txt" count="1"/>

</abc-page> Please see the above XML for details.

Any suggestions

Oh GOD one more bug:-

I am not getting even these

Type , Folder,Author,Comments,Mailed_By,Subject,Key_Words

All i am getting as null -- Please see the above XML/Output for details

Thanks

Message was edited by:

JavaNewUser

JavaNewUsera at 2007-7-12 0:13:02 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 7
I too have a very similar problem as this friend.Can you please help?Thanx in advance
vrkra at 2007-7-12 0:13:02 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 8
No one to help there ....
JavaNewUsera at 2007-7-12 0:13:02 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 9
Got the answer :)
JavaNewUsera at 2007-7-12 0:13:02 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...