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: Parse XML file with java

  1. #1
    Junior Member
    Join Date
    Dec 2017
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Parse XML file with java

    Hello, i am trying to parse an XML file with java :

    Here is the xml file:
    <?xml version="1.0"?>
    <company>
    	<staff id="10">
    		<firstname>john</firstname>
    		<lastname>gerard</lastname>
    		<nickname>kim</nickname>
    		<salary>1000</salary>
    	</staff>
    	<staff id="11">
    		<firstname>jong</firstname>
    		<lastname>lee</lastname>
    		<nickname>yui</nickname>
    		<salary>2000</salary>
    	</staff>
    </company>

    I just want to be able to ask the user: what id does he want to see?
    if id=10, it shows firstname, lastname, nickname and salary about id=10
    if id=11, it shows firstname, lastname, nickname and salary about id=11

    I guess i have to use
    eElement.getAttribute("id") to get the id but how can i give the other infos depending on the id gived before?

    Thank you

  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Parse XML file with java

    What have you tried?

    A simple approach would be to (a) use a DocumentBuilder to build a org.w3c.dom.Document instance from your file. Then you can (b) go through the document to find all the elements with the tag name "staff". (c) You are right: you can use getAttribute("id") to look at an element and see if it is the person you want. (d) Finally you can pull out specific pieces of information from that element's children:

    NodeList nodeList = element.getElementsByTagName("firstName").item(0).getChildNodes();
    Node node = (Node) nodeList.item(0);
    String firstName = node.getNodeValue();

    ---

    I think it might be a good idea to create a simple class for an Employee (a better name than "Staff", I think) and from the document create a List of all the employees. This would help if the user wanted to make multiple searches.

Similar Threads

  1. What is the best way to parse a xml file without xmlns?
    By DemeCarv in forum What's Wrong With My Code?
    Replies: 0
    Last Post: May 26th, 2014, 04:26 PM
  2. How to parse an object to and from XML using JAXB
    By Json in forum File Input/Output Tutorials
    Replies: 1
    Last Post: August 8th, 2013, 05:45 PM
  3. How to parse an XML having same tag.
    By 1bun100 in forum File I/O & Other I/O Streams
    Replies: 12
    Last Post: February 24th, 2012, 08:34 AM
  4. How to parse an object to and from XML using JAXB
    By Json in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: January 20th, 2010, 03:42 AM
  5. How to read an XML document in Java with DOM Parse?
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: May 20th, 2008, 07:04 AM

Tags for this Thread