Results 1 to 8 of 8
- 09-14-2008, 03:59 PM #1
Member
- Join Date
- Sep 2008
- Posts
- 43
- Rep Power
- 0
[Solved] need very basic help with xml parsing in java
I've been looking for a couple of hours on the net and can't find an answer. Thought I'd ask you guys.
If I have a very basic .xml file with 2 fields containing only numbers in each.
Just for simplicities sake at the moment all I want to find out is how to read the 2 fields, convert them to Integers and then add them together as if they are 2 seperate integer values.
I can read the xml elements fine.
I'm assuming they're just strings at the moment, so I assumed I could just use Integer.parseInt
But it won't work. It compiles fine, but throws up an error when I try to output the integer values.
Does anyone know what I'm doing wrong?Last edited by 2potatocakes; 09-17-2008 at 01:25 PM.
- 09-14-2008, 04:46 PM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 25
Can you post your code here to see?
- 09-14-2008, 05:55 PM #3
Member
- Join Date
- Sep 2008
- Posts
- 43
- Rep Power
- 0
Of course, sorry....
Without the:
int difx;
difx=Integer.parseInt(text);
System.out.println(difx);
part, it works fine and outputs a number, but I really need to be able to convert the string into an integer. Can you see what I'm doing wrong?
import java.awt.*;
import javax.swing.*;
import java.io.*;
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("c:\\myjava\\firstgo2.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("employee");
System.out.println("Information of all employees");
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("firstname");
Element fstNmElmnt = (Element) fstNmElmntLst.item(0);
NodeList fstNm = fstNmElmnt.getChildNodes();
System.out.println("First Name : " +
((Node) fstNm.item(0)).getNodeValue());
String text = ((Node) fstNm.item(0)).getNodeValue().toString();
System.out.println(text);
int difx;
difx=Integer.parseInt(text);
System.out.println(difx);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
- 09-14-2008, 05:57 PM #4
Member
- Join Date
- Sep 2008
- Posts
- 43
- Rep Power
- 0
Oh and the xml file is just very basic and looks like this:
<?xml version="1.0" encoding="utf-8"?>
<company>
<employee>
<firstname>1254
</firstname>
<surname>8765
</surname>
</employee>
</company>
- 09-14-2008, 06:28 PM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 25
You have chosen the node in wrong way.
Java Code: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("firstname"); Element fstNmElmnt = (Element) fstNmElmntLst.item(0); NodeList fstNm = fstNmElmnt.getChildNodes(); System.out.println("First Name : " + ((Node) fstNm.item(0)).getNodeValue()); String text = ((Node) fstNm.item(0)).getNodeValue().trim(); System.out.println(text); int difx; difx = Integer.parseInt(text); System.out.println(difx); } }
- 09-15-2008, 07:21 PM #6
Member
- Join Date
- Sep 2008
- Posts
- 43
- Rep Power
- 0
Eranga, you're a legend!
Thanks heaps mate, really appreciate it.
.trim() eh??
never thought to use that before. But again thanks. Works perfect now!:D
- 09-16-2008, 05:28 AM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 25
Do you know what the reason to use trim() instead toString()?
- 09-17-2008, 01:22 PM #8
Member
- Join Date
- Sep 2008
- Posts
- 43
- Rep Power
- 0
Similar Threads
-
Basic Java help, AIM?
By jkswebsite in forum New To JavaReplies: 4Last Post: 07-11-2012, 07:17 PM -
basic java help
By adred in forum New To JavaReplies: 0Last Post: 03-08-2008, 01:36 PM -
help with basic java code
By elizabeth in forum New To JavaReplies: 1Last Post: 08-07-2007, 07:47 PM -
Help with basic shapes in java
By carl in forum Java 2DReplies: 1Last Post: 08-01-2007, 12:40 AM -
Help, basic shapes using java
By coco in forum AWT / SwingReplies: 1Last Post: 07-31-2007, 09:52 PM
Bookmarks