Question

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

Was it helpful?

Solution

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

OTHER TIPS

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>)

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top