tags:

views:

101

answers:

2

Suppose I have a String like this

String from = "<time><day type="tt">ok</day><time>

Now what I would like to do is, create a XOM document and then retrun back something like

String to = documentToString(document)

this string should have only <day type="tt">ok parsed</day>, not with <time>..</time>root element. I have already created the XOM documnet but don't know what is the easy way to do the convert string part.

Thnaks.

A: 

You can use xpath to get the day node:

Nodes nodes = document.query("/time");

You can get the string content of that node with

nodes[0].toXML();
Nathan Hughes
+1  A: 

The toXML() method is your friend:

import nu.xom.*;
import java.io.StringReader;

public class XomElementAsString
{
    public static void main( final String ... args )  throws Exception
    {
        String from = "<time><day type=\"tt\">ok</day></time>";
        Builder parser = new Builder();
        Document document = parser.build( new StringReader( from ) );
        Element child = document
            .getRootElement()
            .getFirstChildElement( "day" );
        System.out.println( child.toXML() );
    }
}

Output:

<day type="tt">ok</day>
Chris Winters