HTML held in DOM (as XML) rendered onto JEditorPane

I am interested to know if anyone out there has any ideas on the following?:
I am holding an html document as a DOM object as it is part of an xml message (which undergoes various transformations) that is passed through our software.
The problem is that to do this I have had to make the html into a 'well-formed' xml document (which doesn't conform to html 3.2)
Basically this causes problems where I want to use html elements that forbid end tags (e.g. <br> or <img>), in that the DOM when written out contains these end tags (so <br> becomes <br></br> or
This actually works fine in IE (as it's quite lenient to these things) but when you put it in a JEditorPane it displays a spare > (not quite sure on it's logic!).
So... does anyone have any good ideas?? My only thought was to extend the HTMLEditorToolKit to be more lenient with these but that strikes me as being a bit hard! I know I can get around it by putting spaces in for things like img (e.g. <img src="bla.gif"> </img> displays fine whereas <img src="bla.gif"></img> displays the image and then a > symbol!)

If anyone is still interested in this tread, an article was written on this subject and ways to work around it:
http://www.xml.com/pub/a/2000/08/30/xsltandhtml

Similar Messages

  • Parsing HTML to get DOM structure

    I have been looking at the various XML libraries such as JTidy, HotSax, Xalan, Tagsoup, htmlparser, etc. trying to find a library which would allow me to parse some HTML, retrieving the DOM structure of the document, without trying to make it any better.
    My goal is to write an application which is able to go through a huge bunch of html templates to modify some parts of it, and since these can be footers, headers, or just pieces of content, I don't want some HTML and BODY tags to be automatically generated...
    Is there any way I could achieve that? All the libraries I tried ended up generating some extra HTML in the DOM structure which I wasn't able to get rid of...

    Well, what I'm doing is a program which can process existing HTML templates so that I can refactor some patterns we have targeted to make everything more uniform.
    Thus I want to be able to read HTML code, alter it, and then produce the result without adding any extra tags guessed by a cleaner. The reason is simple, since the templates are only pieces of a final page, I don't want to end up with <html> tags inside every template piece!
    Oh and it is true that TagSoup is SAX based, but I mixed it with Xalan so that it produces a DOM tree. Here's the resource I found which helped me do that:
    http://www.hackdiary.com/archives/000041.html

  • [Q] convert DOM to XML Document

    Hi,
    Is there any class or library which convert DOM to XML document?
    ----- java program ------------
    import org.w3c.dom.*;
    import org.apache.crimson.tree.XmlDocument;
    public class Sample {
      public static void main(String args[]) {
        // create a document and root element
        Document doc = new XmlDocument();
        Element root = doc.createElement("html");
        doc.appendChild(root);
        // append a data
        Element body = doc.createElement("body");
        root.appendChild(body);
        body.appendChild(doc.createTextNode("Hello"));
        // convert DOM to XML Document
    }----- expected result ----------
    <html>
    <body>
      Hello
    </body>
    </html>Could you help me?

    Hi,
    Look at the package javax.xml.transform
    or
    try the following code:
    I tried the following code.
    ----- java program ------------
    import javax.xml.parsers.*;
    import org.w3c.dom.*;
    import javax.xml.transform.*;
    import javax.xml.transform.dom.*;
    import javax.xml.transform.stream.*;
    import java.io.OutputStream;
    public class Sample {
      public static void main(String[] args) {
        try {
          // create a document and root element
          Document doc = DocumentBuilderFactory.newInstance()
                                .newDocumentBuilder()
                                .newDocument();
          Element root = doc.createElement("html");
          doc.appendChild(root);
          // append a data
          Element body = doc.createElement("body");
          root.appendChild(body);
          body.appendChild(doc.createTextNode("Hello"));
          Element ul = doc.createElement("ul");
          String[] list = {"foo", "bar", "baz"};
          for (int i=0; i<list.length; i++) {
           Element li = doc.createElement("li");
           li.appendChild(doc.createTextNode(list));
         ul.appendChild(li);
    body.appendChild(ul);
    // convert DOM to XML Document
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer();
    //OutputStream stream = new FileOutputStream("output.xml");
    OutputStream stream = System.out;
    transformer.transform(new DOMSource(doc), new StreamResult(stream));
    } catch (Exception ex) {
         ex.printStackTrace();
    ----- result ------------
    <html>
    <body>Hello<ul>
    <li>foo</li>
    <li>bar</li>
    <li>baz</li>
    </ul>
    </body>
    </html>Great! Very thanks!

  • How to get multiple html output file  from an xml document via xslt?

    Hi,
    the purpose is to generate multiple html output file from one xml file
    depending on special tag.
    exp: i have an xml file which contains sevreral articles so how to get each article section in an independant html file
    Thanks for help

    Not a standard feature of XSLT. But Michael Kay's XSLT implementation, SAXON, provides that as an extension. Get it here:
    http://saxon.sourceforge.net/

  • XML rendering in IE and Netscape

    Hi all,
    I have a simple servlet that streams xml data to the browser. When I view the page in IE it is formatted in the DHTML tree structure but with Netscape 7 it is messed up. However when I open an xml document directly in Netscape, it works! My JSP page has an IFrame that streams the XML data. Can someone let me know why XML rendering does not work in Netscape but in IE? Do I need to set some browser specific parameter in my servlet?
    Many thanks
    clementv

    Hi,
    Iam setting the content type as 'text/xml'.
              byte[] bytedata = null;
              resp.setContentType(CONTENT_TYPE);
                   if (mBean != null && mBean.getParsedXMLContent() != null ) {
                        bytedata = mBean.getParsedXMLContent().getBytes();
                        //Create Servlet Output Stream
                        sos = resp.getOutputStream();
                        sos.write(bytedata);
                   }

  • Oracle 8i - XML rendering

    The XML twp and iFS overview refer to XML parsing and rendering
    in relation to iFS. If we are interested in XML rendering only,
    do we require iFS ?
    TIA
    Ronny
    null

    If you need XML Rendering only, you will need to wait for our XSL
    Processor which is currently in development. No announced time
    for beta as yet.
    Ronny Weisz (guest) wrote:
    : The XML twp and iFS overview refer to XML parsing and rendering
    : in relation to iFS. If we are interested in XML rendering only,
    : do we require iFS ?
    : TIA
    : Ronny
    Oracle XML Team
    http://technet.oracle.com
    Oracle Technology Network
    null

  • FDF error while loading xml file onto PDF

    Need some help here folks. Im getting a FDF error while loading some value into pdf form. It not happening all the time, 1 out of 60 times with the same xml file onto a new pdf form. Anyone know why this happening.  Is there some technical support i can purchase to get this resolved.

    9.1  and 9.3.4
    Date: Fri, 5 Aug 2011 00:56:19 -0600
    From: [email protected]
    To: [email protected]
    Subject: FDF error while loading xml file onto PDF
    Could you let me know which version of Adobe Reader are you using?
    >

  • Can anyone help me in converting a dom to xml

    hi,
    i am facing a problem in using transformer factory to convert a dom to xml.
    please help me.
    if anyone has a sample code in java please share with me.

        static public void serialize( Document doc, File outFile)
            try
               TransformerFactory tfFac = TransformerFactory.newInstance();
               Transformer tf = tfFac.newTransformer();
               // tf.setOutputProperty("indent", "yes");
               tf.transform(new DOMSource(doc),
                  new StreamResult(new FileWriter( outFile )
            }         // end of try
            catch( FileNotFoundException fnfe )
               System.err.println( "File Creation Error in serializer" );
               System.err.println( fnfe.getMessage() );
            }         // end of catch
            catch (TransformerException e)
                e.printStackTrace();
            }         // end of catch
            catch( IOException ioe )
               System.err.println( "IOException in serializer" );
               System.err.println( ioe.getMessage() );
            }         // end of catch
        }Dave Patterson

  • How to display Xml in a JEditorPane(in an applet)

    Hi All,
    I have an applet and i want to display xml in a JEditorPane in an applet. This xml document has a xsl attached to it for transformation. How can i do the transformation??
    Help or sample code or link to a sample code is highly appreciated!!!!
    Thanks
    Ragu

    Look this servlet which does a transformation. JEditorPane could call this servlet (with the method setPage) to do the transformation:
    public class SampleXSLTServlet extends javax.servlet.http.HttpServlet {
    public final static String FS = System.getProperty("file.separator");
    // Respond to HTTP GET requests from browsers.
    public void doGet (javax.servlet.http.HttpServletRequest request,
    javax.servlet.http.HttpServletResponse response)
    throws javax.servlet.ServletException, java.io.IOException
    // Set content type for HTML.
    response.setContentType("text/html; charset=UTF-8");
    // Output goes to the response PrintWriter.
    java.io.PrintWriter out = response.getWriter());
    try
    javax.xml.transform.TransformerFactory tFactory =
    javax.xml.transform.TransformerFactory.newInstance();
    //get the real path for xml and xsl files.
    String ctx = getServletContext().getRealPath("") + FS;
    // Get the XML input document and the stylesheet, both in the servlet
    // engine document directory.
    javax.xml.transform.Source xmlSource =
    new javax.xml.transform.stream.StreamSource
    (new java.net.URL("file", "", ctx+"foo.xml").openStream());
    javax.xml.transform.Source xslSource =
    new javax.xml.transform.stream.StreamSource
    (new java.net.URL("file", "", ctx+"foo.xsl").openStream());
    // Generate the transformer.
    javax.xml.transform.Transformer transformer =
    tFactory.newTransformer(xslSource);
    // Perform the transformation, sending the output to the response.
    transformer.transform(xmlSource,
    new javax.xml.transform.stream.StreamResult(out));
    // If an Exception occurs, return the error to the client.
    catch (Exception e)
    out.write(e.getMessage());
    e.printStackTrace(out);
    // Close the PrintWriter.
    out.close();

  • Modify HTML of the document of the rendered page and reload

    Currently i am loading a url in javafx through webengine.load(url). My requirement is to keep the styling in tact with the original page. However, once the page is rendered, the fonts are not loaded and i am unable to increase the font sizes. I worked through the following steps to achieve this.
    1. Add a listener through getLoadWorker method for web engine and get the document object when the state is SUCCEEDED.
    2. Transform the document to a html string which gives me the whole HTML of the page in a string including the css.
    3. Then i do a replace on the css part with the actual location of the font files (absolute path url) and reload the html through loadContent method.
    With this, i am able to get the fonts loaded properly.
    Problem:
    1. I end up with a infinite loop when i use the "webEngine.loadContent(string, "text/html")" inside the getLoadWorker method.
    2. I tried to do that outside the getLoadWorker method before the webEngine.load(url) but in this case the replace html body string is coming as null.
    Any help on how to achieve this?? Below is my code:
          webEngine.getLoadWorker().stateProperty().addListener(new ChangeListener<Worker.State>() {
                String htmlBody = null;
                @Override
                public void changed(ObservableValue<? extends Worker.State> observableValue, Worker.State prevState, Worker.State newState) {
                    //To change body of implemented methods use File | Settings | File Templates.
                    //String htmlBody = null;
                    int count = 0;
                    if (newState == Worker.State.SUCCEEDED) {
                        browser.requestFocus();
                        // Get the document object from Engine.
                        Document doc = webEngine.getDocument();
                        try {
                            // Use Transformer to convert the HTML Object from the document top String format.
                            Transformer transformer = TransformerFactory.newInstance().newTransformer();
                            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
                            transformer.setOutputProperty(OutputKeys.METHOD, "html");
                            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
                            transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
                            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
                            StringWriter outWriter = new StringWriter();
                            transformer.transform(new DOMSource(doc),new StreamResult(outWriter));
                            StringBuffer sb = outWriter.getBuffer();
                            htmlBody = sb.toString();
                            // Replace the font-family attribute in the style section to the actual URL of the font being used.
                            htmlBody = htmlBody.replace("font-family: medium", "font-family: url(http://1.10.30.45:8080/fonts/Md.ttf)");
                            // Load the new HTML string to the Engine.
                            //webEngine.loadContent(htmlBody, "text/html");
                        } catch (Exception ex) {
                            ex.printStackTrace();
            webEngine.load(url);
            //add the web view to the scene
            getChildren().add(browser);

    James,
    I tried the document manipulation but was unable to override the css that was already applied. I downloaded and used jdk 8 which has the latest version of javaFX which fixed the font loading issue. Now i can see all the custom fonts and everything getting loaded as part of css.
    However, when i try to capture the state of the worker, it stays in the running and not changing to success. First few instances, the page was loaded successfully but then it stopped. Any idea if this is due to the new javaFX version of jdk8? Below is the code snippet:
           webEngine.getLoadWorker().stateProperty().addListener(new ChangeListener<Worker.State>() {
                @Override
                public void changed(ObservableValue<? extends Worker.State> observableValue, Worker.State prevState, Worker.State newState) {
                    //To change body of implemented methods use File | Settings | File Templates.
                    System.out.println(newState);
                    if (newState == Worker.State.SUCCEEDED) {
                        browser.requestFocus();
                        // Get the document object from Engine.
                        Document doc = webEngine.getDocument();
                        System.out.println(doc.getDocumentURI());
            webEngine.load(url);
            //add the web view to the scene
            getChildren().add(browser);
    The output is
    SCHEDULED
    RUNNING
    and nothing after that...

  • Java servlet, DOM and xml help! please urgent!

    I have to write a servlet that takes the user input to build a xml document using DOM API and should be able to append new entry to that xml file whenever any user adds an entry. I have written the fillowing code, can anyone please take a look at this code and tell me the right way to do it as this code is actually not creating any xml file and also in commented line of this code I tried by giving blank xml file name with just root element, so that this code can enter new elements like name, title as child element of the root, but doesn't work.
    PrintWriter out = res.getWriter ();
    res.setContentType ("text/html");
    try
    Document xmldoc = null;
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    DOMImplementation impl = builder.getDOMImplementation();
    // Document xmldoc = builder.parse(xmlfile name);
    Element e = null;
    Node n = null;
    String name = req.getParameter("name");
    String title = req.getParameter("title");
    String address = req.getParameter("address");
    xmldoc = impl.createDocument(null, "records", null);
    Element root = xmldoc.getDocumentElement();
    e = xmldoc.createElementNS(null, "name");
    n = xmldoc.createTextNode(name);
    e.appendChild(n);
    root.appendChild(e);
    e = xmldoc.createElementNS(null, "title");
    n = xmldoc.createTextNode(title);
    e.appendChild(n);
    root.appendChild(e);
    e = xmldoc.createElementNS(null, "address");
    n = xmldoc.createTextNode(address);
    e.appendChild(n);
    root.appendChild(e);
    DOMSource domSource = new DOMSource(xmldoc);
    Source xslSource = new StreamSource (xsl file);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer(xslSource);
    transformer.transform(domSource,new StreamResult (out));

    Document xmldoc = null;
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    xmldoc = documentBuilder.newDocument();
    Element e = null;
    Node n = null;
    String name = req.getParameter("name");
    String title = req.getParameter("title");
    String address = req.getParameter("address");
    e = xmldoc.createElement("name");
    n = xmldoc.createTextNode(name);
    e.appendChild(n);
    xmldoc.appendChild(e);
    e = xmldoc.createElement("title");
    n = xmldoc.createTextNode(title);
    e.appendChild(n);
    xmldoc.appendChild(e);
    e = xmldoc.createElement("address");
    n = xmldoc.createTextNode(address);
    e.appendChild(n);
    xmldoc.appendChild(e);
    DOMSource domSource = new DOMSource(xmldoc);
    StreamSource xslSource = new StreamSource (xsl file);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer(xslSource);
    transformer.transform(domSource,new StreamResult (out));

  • DOM to XML String

    Hi,
    I'm trying to transform a DOM object into a string of xml, but somehow it only outputs the first line.
    What did I do wrong?
              Document doc = processDoc();
              Source source = new DOMSource((Element) doc.getElementsByTagName("vancouver").item(0));
              StringWriter out = new StringWriter();
              StreamResult result = new StreamResult(out);
              Transformer xformer = TransformerFactory.newInstance().newTransformer();
              xformer.setOutputProperty("encoding", "iso-8859-1");
              xformer.setOutputProperty("indent", "yes");
              xformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
              xformer.transform(source, result);
              return result.getWriter().toString();Thanks!
    Will

    Hi DrClap,
    this is how I constructed my DOM object:
         public Document processDoc() throws Exception {
              URL u = new URL("http://www.weatheroffice.ec.gc.ca:80/city/pages/bc-74_metric_e.html");
              DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
              DocumentBuilder builder = factory.newDocumentBuilder();
              // in doc
              Document inDoc = builder.parse(u.openStream());
              // out doc
              Document outDoc = builder.newDocument();
              // grab <div id="citytextf">
              Element inElement = inDoc.getElementById("citytextf");
              Element rootElement = inElement;
              // grab <h2>
              inElement = (Element) inElement.getElementsByTagName("h3").item(0);
              System.out.println(inElement.getTextContent());
              // create output root element
              Element outElement = outDoc.createElement("vancouver");
              outElement.setAttribute("issued", inElement.getTextContent().substring(11));
              rootElement = (Element) rootElement.getElementsByTagName("dl").item(0);
              // iterate thru city <dd>, <dt> tags
              NodeList tempList = rootElement.getElementsByTagName("dt");
              Element tempIn, tempOut;
              for (int i = 0; i < tempList.getLength(); i++) {
                   System.out.println(i);
                   tempIn = (Element) rootElement.getElementsByTagName("dt").item(i);     // grabs <dt> value
                   tempOut = outDoc.createElement("day");
                   tempOut.setAttribute("name", tempIn.getTextContent().trim());
                   tempIn = (Element) rootElement.getElementsByTagName("dd").item(i);
                   String input = tempIn.getTextContent();
                   (tempOut.appendChild(outDoc.createElement("high"))).setNodeValue(getHighLowValue("High", input));
                   (tempOut.appendChild(outDoc.createElement("low"))).setNodeValue(getHighLowValue("Low", input));
                   (tempOut.appendChild(outDoc.createElement("note"))).setNodeValue(getNoteValue(input));
                   System.out.println(((Element) tempOut).getAttribute("name"));
                   outElement.appendChild(tempOut);
              return outDoc;
         }which is suppose to output XML like this:
    <vancouver issued="Issued 6.30 PM PDT Saturday 16 April 2005">
      <day name="Tonight">
        <high></high>
        <low>6</low>
        <note>Cloudy. 60 percent chance of showers. Risk of small hail this evening.</note>
      </day>
      <day name="Sunday">
        <high>12</high>
        <low></low>
        <note>A few showers.</note>
      </day>
      <day name="Monday">
        <high>13</high>
        <low>6</low>
        <note>A mix of sun and cloud.</note>
      </day>
      <day name="Tuesday">
        <high>13</high>
        <low>6</low>
        <note>Sunny</note>
      </day>
      <day name="Wednesday">
        <high>15</high>
        <low>7</low>
        <note>Sunny</note>
      </day>
    </vancouver>
          But all it outputs is:
    <?xml version="1.0" encoding="iso-8859-1"?>I just started learning XML in Java so there're probably things that I missed out on.
    Thanks!

  • Render HTML inside Excel, using Open XML

    Hello guys,
    I am creating excel file using, Open XML.
    The data will be coming from database, and can contain HTML tags.
    e.g. <b>some sample <i style='color:red'> text </i></b>
    I want the HTML to be rendered inside excel sheet, so that user can view well formatted excel..
    Please, Help me out

    Hi J_Prasanna,
    I'm afraid that it's not possible with OpenXML SDK, but it's possible if you use Excel PIA along with Internet Explorer. Use the Internet Explorer COM object to render the HTML content, then copy the document, use the Paste method of the Worksheet object
    to paste the text with format.
    Dim IE As Object
    Set IE = CreateObject("InternetExplorer.Application")
    With IE
    .Visible = False
    .Navigate "about:blank"
    .document.body.InnerHTML = Sheets("Sheet1").Range("A1").Value
    .document.body.createtextrange.execCommand "Copy"
    ActiveSheet.Paste Destination:=Sheets("Sheet1").Range("A1")
    .Quit
    End With
    The code is similar if you use managed project.
    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click
    HERE to participate the survey.

  • Transform DOM as XML ????????

    Hi Friends
    I have created a DOM for an XML file and then I edited that DOM.
    Now Please if it is possible to TRANSFORM that DOM again as an XML file instead of HTML file.
    One mode thing if i have a dom and I want to add CDATA to specific NODE how do we do it .. Please let me know.
    Any help would be great.
    Thanks

    How to add a CDATA section to a NODE, provided that the DTD allows a CDATA section to be added to a NODE you can do the following:
    This is the general idea: you will have to check if it compiles.
    //Create a CharacterData node from a Document. The document represents a DOM which was either created from an xml file or is being created on the file.
    CharacterData cdata = document.createCDATASection("The character data");
    //Find the node that you want by using a search algorithm:
    public boolean findNode(Node root, String elementName,
    CharacterData cdata){
    if(root instanceof Text){
    System.out.println("element is a text node");
    return false;
    //if you have found the node with local name ==
    //elementName. Then append the cdatasection as a
    //child
    if(root.getLocalName().equals(elementName)){
    System.out.println("Found Node");
    root.appendChild(cdata);
    return true;
    if(!root.hasChildNodes()){
    System.out.println("element has no children");
    return false;
    //Run through the children for root and for each //recursively see if any of their children is the desired node.
    NodeList childNodes = root.getChildNodes();
    int noChildren = childNodes.getLength();
    boolean result = false;
    for(int i = 0; i<noChildren; i++){
    if(!result){
    System.out.println("About to find next child");
    result = findNode(childNodes.item(i),
    elementName, cdata);
    }else break;
    return result;
    Hope this helps.

  • HTML bold text, imported from XML problem.

    I have HTML content defined in an external XML file:
    quote:
    <textBody><p>First line of text containing some
    <b>bold words</b>. More text with some <b>bold
    words</b> and then two line breaks.<br /><br
    />Here's some more text with <b>some more bold
    words</b> in the middle of it. Don't you think there's
    something strange about the spacing around these <b>bold
    words</b>? It seems to get worse with each extra appearance
    of a <b>bold word</b>. Thank you <b>for
    reading</b> my text.</p></textBody>
    Here,
    you can see a screenchot of the text rendered on screen from the
    HTML above.
    Many thanks for any help from anybody.

    We think we've got the source of the problem - because the
    textfield displaying the HTML is in a component. We believe the
    letter spacing issue comes about from the component being resized.
    The solution, as far as we can see is to simply use dynamic
    textfields instead of components - OK for future development, but
    what we've done already will need to be fixed by extra spaces in
    various places.
    Thanks for your suggestion Pal_w_g - guess it was your input
    that provided the seed that led to the idea about the component.

Maybe you are looking for