Transform XML JDOM to String

Posted on 2004-11-16
Last Modified: 2013-11-23
I have a program in which I've created a JDOM document with various elements.  I want to take the JDOM Document and extract the stuff inside the element tags ( these things <>) as a string.  Any suggestions?

I don't know if I need to use the JDOM transformer or the javax.xml transformer.  I don't know how to use either one.  Any help would be fantastic.

Here's some of my code:

Document xml;

Element root = new ELement("Login");
Document doc = new Document(root);

I'm getting this when I print the document to the console:

I need to get myName in a string format.
Question by:trevorhigbee2000
    LVL 21

    Expert Comment

    You can either taverse the JDOM document or write a XSL stylesheet.  Traversal should be easier
      Element loginElement=(Element)doc.getChild("Login");
      String loginValue =  loginElement.getText();

    To use the transformer, I would suggest using the JAXP API.  It will work if you decide to use the W3C DOM with no changes:

       StreamSource xslSource=new StreamSource(new File("getLogin.xsl"));
       Transformer t=TransformerFactory.newInstance().newTransformer(xslSource);

       Source xmlSource=new JDOMSource(doc);
       Result out=new StreamResult(System.out);
       t.transform(xmlSource, out);

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="" xmlns:fo="">
        <xsl:template match='/'>
              <xsl:value-of select='//Login'/>

    LVL 2

    Author Comment

    When I do:

    Element loginElement=(Element)doc.getChild("Login");

    I get an error:
    "The method getChild(String) is undefined for the type Document."
    LVL 6

    Accepted Solution


    you first need to get the element (node) from the document, then only you can access the individual properties and/or child nodes.  so do this...

    Element root = doc.getRootElement();
    String childText = root.getChildText( "Login" );


    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Which XML parser should I used for my requirement 11 49
    countX 22 50
    word0 challenge 4 37
    java set up 1 37
    For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
    Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
    Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
    This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now