Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Basic XML Reading Question

  1. #1
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Basic XML Reading Question

    I have what should be an extremely easy XML question. I'm not sure why I can't figure it out on my own.

    Okay, so I have an XML node which looks like this:
    <spend>
    	<metal>1</metal>
    	<rope>1</rope>
    </spend>

    I need to get each node within the spend tag. This is what I tried to do:
    Element spendElement = ...;
    NodeList spendList = spendElement.getChildNodes();
    for(int i=0;i<spendList.getLength();i++) {
    	Node spendResource = spendList.item(i);
    	logger.info("Node Name: "+spendResource.getNodeName());
    	...
    }
    But that prints out:
    Node Name: #text
    NOTE: an exception is thrown later due to the name, so the program doesn't print the second line

    I would like it to say:
    Node Name: metal
    Node Name: rope

    Thoughts?

    EDIT: I'm using org.w3c.dom, btw.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/


  2. #2
    Junior Member
    Join Date
    Sep 2014
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Basic XML Reading Question

    Hi I'm just getting into XML parsing myself and I'm probably way behind you in this, however I Think you ask for the wrong information. Try asking for the classnames of these childnodes and put them in spendList instead of the nodeName Property.

    The nodeName Property

    The nodeName property specifies the name of a node.
    •nodeName is read-only
    •nodeName of an element node is the same as the tag name
    •nodeName of an attribute node is the attribute name
    •nodeName of a text node is always #text <---------------------This is what you are doing
    •nodeName of the document node is always #document

    Note: nodeName always contains the uppercase tag name of an HTML element

    I hope this helps somewhat.

Similar Threads

  1. Reading Web XML Document
    By Trolltrain in forum Java Theory & Questions
    Replies: 2
    Last Post: August 12th, 2014, 02:43 PM
  2. [SOLVED] Reading ip and port from xml file
    By learn_java in forum Java Networking
    Replies: 0
    Last Post: November 19th, 2012, 09:08 AM
  3. Reading XML File using DOMParser and have problem with accessing xml
    By optiMystic23 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 21st, 2012, 02:22 PM
  4. Reading XML, isEndElement is never true
    By Angela in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 12th, 2010, 12:36 PM
  5. Anyone Help me in XML file Reading??????????
    By goplrao in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: May 2nd, 2010, 11:04 AM