quarks
quarks

Reputation: 35326

Parse Xml String with Joox

How do I parse a Xml String with jOOx? The parse method accepts String uri, but not Xml String.

Upvotes: 1

Views: 2372

Answers (2)

kakoni
kakoni

Reputation: 5130

In the simplest form it looks like this

// Parse the document from string
Document document = $(new StringReader("<?xml version='1.0'?><root><child attr='attr' /></root>")).document();

And then do your matching with

// Wrap the document with the jOOX API and find root element
Match m = $(document).find("root");

Upvotes: -1

Dave Jarvis
Dave Jarvis

Reputation: 31191

Looks like you can do it using the JOOX class and a StringReader. For example:

String xml = "<?xml version='1.0'?><root><child attr='attr' /></root>";
StringReader sr = new StringReader( xml );
Match m = JOOX.builder().parse( sr );

See the DocumentBuilder API, which JOOX uses.

You might have to convert the StringReader to an InputSource or equivalent, but that's a relatively trivial conversion. From the test case:

    xmlExampleString = IOUtil.toString(JOOXTest.class.getResourceAsStream("/example.xml"));
    xmlExampleDocument = builder.parse(new ByteArrayInputStream(xmlExampleString.getBytes()));

In this case, you could write:

String xml = "<?xml version='1.0'?><root><child attr='attr' /></root>";
StringReader sr = new StringReader( xml );
ByteArrayInputStream bais = new ByteArrayInputStream( new InputStreamReader( sr ) );
document = builder.parse( bais );

That should get you started. Note that there are probably simpler ways to convert a String to an input stream.

Upvotes: 4

Related Questions