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 4 of 4

Thread: Java and XML

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    1
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Java and XML

    Hi .. I am doing a project in JAVA where i have to parse an XML document.
    In the document for eg.
    <book>harrypotter</book>

    i am able to read the element name 'book'. But i do not know hoe to read the value 'harrypotter' . I would like to know the java statement that will help me read it.
    Can anyone please help me with it.


  2. #2
    Junior Member
    Join Date
    Jan 2010
    Location
    Orpington, Kent, UK
    Posts
    18
    Thanks
    0
    Thanked 9 Times in 8 Posts

    Default Re: Java and XML

    You need to use an XML Parser to parse the file and providing the XML is 'well-formed' you will be able to navigate the XML structure.
    I would recommend you use a DOM parser , this will give you access to a DOM (Document Object Model) object which will then enable you to navigate through the structure.
    Look at documentation on JAXP (Java API for XML Processing) on how to do this.

    This link should get you started "http://en.wikipedia.org/wiki/Java_API_for_XML_Processing"

    Have fun!
    Last edited by JavaDaveUK; February 6th, 2010 at 07:14 PM. Reason: Oops!

  3. The Following User Says Thank You to JavaDaveUK For This Useful Post:

    anjanavkumar (February 7th, 2010)

  4. #3
    Junior Member
    Join Date
    Feb 2010
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java and XML

    String Xml="<book>harrypotter</book>";
    Document dom = DocumentHelper.parseText(Xml);
    Node node=dom.selectSingleNode("//book"); //navigate the xml structure by Xpath,"//" means throuth root
    String value=node.getText();

    You must import the dom4j.jar first;

  5. #4
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Java and XML

    Have a look at JDOM

    // Json