Results 1 to 8 of 8
  1. #1
    2potatocakes is offline Member
    Join Date
    Sep 2008
    Posts
    43
    Rep Power
    0

    Smile [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 12:25 PM.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  3. #3
    2potatocakes is offline Member
    Join Date
    Sep 2008
    Posts
    43
    Rep Power
    0

    Default 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();
    }
    }
    }

  4. #4
    2potatocakes is offline Member
    Join Date
    Sep 2008
    Posts
    43
    Rep Power
    0

    Default

    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>

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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);
                }
            }

  6. #6
    2potatocakes is offline Member
    Join Date
    Sep 2008
    Posts
    43
    Rep Power
    0

    Default 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

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  8. #8
    2potatocakes is offline Member
    Join Date
    Sep 2008
    Posts
    43
    Rep Power
    0

    Default

    I didn't before you posted your last message. But I looked it up. Just found out, it trims the whitespaces off the beginning and the end of the string and creates a substring.

    Very handy, thanks again.

Similar Threads

  1. Basic Java help, AIM?
    By jkswebsite in forum New To Java
    Replies: 4
    Last Post: 07-11-2012, 06:17 PM
  2. basic java help
    By adred in forum New To Java
    Replies: 0
    Last Post: 03-08-2008, 12:36 PM
  3. help with basic java code
    By elizabeth in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 06:47 PM
  4. Help with basic shapes in java
    By carl in forum Java 2D
    Replies: 1
    Last Post: 07-31-2007, 11:40 PM
  5. Help, basic shapes using java
    By coco in forum AWT / Swing
    Replies: 1
    Last Post: 07-31-2007, 08:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •