Results 1 to 4 of 4
- 08-17-2010, 07:42 PM #1
Member
- Join Date
- Aug 2010
- Posts
- 2
- Rep Power
- 0
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.
this is application code:Java Code:<?xml version="1.0" encoding="UTF-8"?> <characters> <character value='a' /> </characters>
I'm feeling really ashamed to ask this question, but like I said I'm desperate. There is probably something I'm overlooking.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(); }
If this isn't the correct code, than how do I get value from "value" attribute
Tnx for your answers.
-
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()); } }
- 08-17-2010, 10:20 PM #3
Member
- Join Date
- Aug 2010
- Posts
- 2
- Rep Power
- 0
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.
-
Similar Threads
-
Question mark colon operator question
By orchid in forum Advanced JavaReplies: 9Last Post: 12-19-2010, 08:49 AM -
novice question about output
By GarBage in forum New To JavaReplies: 1Last Post: 02-05-2010, 01:08 PM -
java question output file
By dazednconfused in forum New To JavaReplies: 3Last Post: 09-17-2009, 03:07 PM -
Question about File renameTo() method : Abnormal output
By suvojit168 in forum New To JavaReplies: 1Last Post: 07-25-2009, 08:22 PM -
Java, output string, getting correct output? HELP!
By computerboyo in forum New To JavaReplies: 2Last Post: 02-25-2009, 11:44 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks