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
# 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
# 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