Trent Gm
Trent Gm

Reputation: 2477

How to insert XHTML into XML

I'm working with JDOM at the moment. I can't think of a solution which what should essentially be an easy problem.

I have a valid XHTML string:

<b>M&amp;A</b> &euro;

How do I insert this into the XML DOM as follows?

<parentNode>
  <b>M&amp;A</b>
  €
</parentNode>

(this XML then goes off to an XSL transformer, which then renders XHTML for the browser)

I've come up with the following 'pseudo' solutions, but I'm not sure if they're possible:

Unescape entities which aren't XML entities, then insert.
Reinscape only XML entites, then HTML unescape the entire string, then insert.

Taras

Upvotes: 2

Views: 1117

Answers (3)

George Bina
George Bina

Reputation: 1161

Create a string containing

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>

+

your XHTML content, in this case <b>M&amp;A</b> &euro;

+

</html>

and then parse this string to obtain a document. Then get all the content inside the root element, that will be your XHTML content and place it inside your parentNode element. You may need to take into account that the content comes from a different document.

Upvotes: 0

Tomalak
Tomalak

Reputation: 338208

I guess you can use JTidy to transform named entities to numbered ones. After that, the XHTML is also valid XML.

Upvotes: 2

drdaeman
drdaeman

Reputation: 11471

While &euro; is valid XHTML entity it is not valid XML one.

Unfortunately, I don't know anything about JDOM, but if it is possible you may try adding DTD entity declarations like <!ENTITY euro "€">. And, maybe, put all XHTML tags into their proper namespace (<parentNode xmlns:x="http://www.w3.org/1999/xhtml"><x:b>...</x:b></parentNode>)

Upvotes: 0

Related Questions