Results 1 to 4 of 4
  1. #1
    ivansla is offline Member
    Join Date
    Aug 2010
    Posts
    2
    Rep Power
    0

    Default XML no output. probably ridiculous question

    Ok guys, I have been tryin to figure out what's wrong for several hours now, and I'm really desperate. This is probably stupid question but, why doesn't my application show node value? The output is null.

    this is xml.

    Java Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <characters>
    	<character value='a' />
    </characters>
    this is application code:

    Java Code:
    try {
    			File file = new File("Characters.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());
    			//System.out.println("Childs: " + doc.getDocumentElement().getChildNodes().getLength());
    			NodeList nodeList = doc.getElementsByTagName("character");
    			//System.out.println(nodeList.getLength());
    			//System.out.println("Information of all characters");
    
    			 for (int s = 0; s < nodeList.getLength(); s++) {
    			
    			
    			 Node node = nodeList.item(s);
    			 System.out.println("Character: " + node.getNodeValue());
    			 }
    
    			
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    I'm feeling really ashamed to ask this question, but like I said I'm desperate. There is probably something I'm overlooking.
    If this isn't the correct code, than how do I get value from "value" attribute

    Tnx for your answers.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Have you tried getting the node's attributes and then extracting the information from them:
    Java Code:
             for (int s = 0; s < nodeList.getLength(); s++) {
    
                Node node = nodeList.item(s);
                //System.out.println("Character: " + node.getNodeValue());
                NamedNodeMap nodeMap = node.getAttributes();
                for (int i = 0; i < nodeMap.getLength(); i++) {
                   Node nodeMapNode = nodeMap.item(i);
                   System.out.printf("%s: %s%n", nodeMapNode.getNodeName(), nodeMapNode.getNodeValue());
                }
             }

  3. #3
    ivansla is offline Member
    Join Date
    Aug 2010
    Posts
    2
    Rep Power
    0

    Default

    Hey thanks man. I tried what you suggested and it works.
    But still, I wonder why it didn't work in the first place.

    Once again thanks.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by ivansla View Post
    Hey thanks man. I tried what you suggested and it works.
    You're welcome.


    But still, I wonder why it didn't work in the first place.
    What do you feel is the value of this node: <character value='a' />? This gets to the core difference between a value and an attribute.

Similar Threads

  1. Question mark colon operator question
    By orchid in forum Advanced Java
    Replies: 9
    Last Post: 12-19-2010, 09:49 AM
  2. novice question about output
    By GarBage in forum New To Java
    Replies: 1
    Last Post: 02-05-2010, 02:08 PM
  3. java question output file
    By dazednconfused in forum New To Java
    Replies: 3
    Last Post: 09-17-2009, 04:07 PM
  4. Question about File renameTo() method : Abnormal output
    By suvojit168 in forum New To Java
    Replies: 1
    Last Post: 07-25-2009, 09:22 PM
  5. Java, output string, getting correct output? HELP!
    By computerboyo in forum New To Java
    Replies: 2
    Last Post: 02-26-2009, 12:44 AM

Posting Permissions

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