logo       


Re: [xmlc] Adding xhtml code in string form: msg#00035

Subject: Re: [xmlc] Adding xhtml code in string form
I figured I should contribute SOMETHING to this list
that I've gotten so much out of.  I hate googling for
a problem and finding an email list archive where the
person just says, "Nevermind, I've figured it out,"
without posting the solution.  Jerks!  :-)

To be honest, the solution I posted doesn't quite
work.  The correct solution is this:

---
// same imports as previous message

public static final String XML_HEADER = "<?xml
version=\"1.0\" encoding=\"ISO-8859-1\"?>\n";

public static final void addXhtmlToNode(final
XMLObject page, final Node destNode, final String src)
throws IOException, SAXException
{
        final StringBuilder xml = new
StringBuilder(XML_HEADER);
        xml.append("<span>");
        xml.append(src);
        xml.append("</span>");
        final DOMParser parser = new DOMParser();
        parser.parse(new InputSource(new
ByteArrayInputStream(xml.toString().getBytes())));
        final Document document = parser.getDocument();
        final NodeList nodeList =
document.getFirstChild().getChildNodes();
        for (int i = 0; i < nodeList.getLength(); i++)
        
destNode.appendChild(page.importNode(nodeList.item(i),
true));
}
---

The differences:

1) Leave out the xhtml part of the header, because it
requires an <html> tag.
2) The <span> magic around the xhtml string.  This
will allow for xhtml code that doesn't start with a
tag, like "This is some <b>bold</b> text".  It worked
before for my unordered list because my xhtml code
started with a <ul> tag.

May your time be saved by this example!

Erik

P.S.  What kind of idiot made the NodeList class that
doesn't implement any java.util collection interfaces?
 Would an iterator() method have been so hard?  I
mean, getLength() and item()???

--- Justin Akehurst <akehurst@xxxxxxxxx> wrote:

> Cool! Thanks for posting how to do this. :)
> 
> -Justin
> 
> On Sep 5, 2005, at 7:01 AM, Erik Rasmussen wrote:
> 
> > Here's a solution I came up with:
> >
> > ---
> > import org.apache.xerces.parsers.DOMParser;
> > import org.enhydra.xml.xmlc.XMLObject;
> > import org.w3c.dom.Document;
> > import org.w3c.dom.Node;
> > import org.xml.sax.InputSource;
> > import org.xml.sax.SAXException;
> >
> > import java.io.ByteArrayInputStream;
> > import java.io.IOException;
> >
> > // ...
> >
> > public static final String XML_HEADER = "<?xml
> > version=\"1.0\" encoding=\"ISO-8859-1\"?>\n" +
> >             "<!DOCTYPE html PUBLIC \"-//W3C//DTD
> XHTML 1.0
> > Transitional//EN\"
> >
>
\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\";>\n";
> >
> > public static final void addXhtmlToNode(final
> > XMLObject page, final Node destNode, final String
> src)
> > throws IOException, SAXException
> > {
> >     final StringBuilder xml = new
> > StringBuilder(XML_HEADER);
> >     xml.append(src);
> >     final DOMParser parser = new DOMParser();
> >     parser.parse(new InputSource(new
> > ByteArrayInputStream(xml.toString().getBytes())));
> >     final Document document =
> parser.getDocument();
> >
> >
>
destNode.appendChild(page.importNode(document.getFirstChild(),
> > true));
> > }
> >
> > // ...
> > ---
> >
> > Works fine.  As long as I validate all the xml
> going
> > into the database, the stuff coming out of it
> should
> > work here.
> >
> > Erik
> >
> >
> > --- Justin Akehurst <akehurst@xxxxxxxxx> wrote:
> >
> >
> >> Perhaps if you can find out how you can take a
> >> string of xhtml text
> >> and suck that into a DOM Document format, then
> you
> >> could probably do
> >> DOM import and import that chunk into your
> document
> >> somehow.  Would
> >> probably make sense if your chunk in the database
> >> was a fully valid
> >> XHTML chunk, though.
> >>
> >> -Justin
> >>
> >> On Sep 4, 2005, at 5:24 PM, i_am_erik@xxxxxxxxx
> >> wrote:
> >>
> >>
> >>> Hello again.  I have a field in my database that
> >>>
> >> allows xhtml, and
> >>
> >>> I want to be able to write this to the page. 
> This
> >>>
> >> was
> >>
> >>> straightforward in jsp-land.  But how do I do it
> >>>
> >> in xmlc-land?
> >>
> >>> Obviously, calling the setTextWhatever() method
> is
> >>>
> >> escaping all my
> >>
> >>> code.  So I guess I need to construct a dom
> >>>
> >> structure from my
> >>
> >>> string value, and appendChild() that.  But how?
> >>>
> >> I'm sure I could
> >>
> >>> dig into the depths of DocumentLoaderImpl.java
> and
> >>>
> >> Xerces figure it
> >>
> >>> out, but I thought I'd pose the question here
> >>>
> >> first.  Surely this
> >>
> >>> has been done by some developer on this list.
> >>>
> >>> Cheers,
> >>> Erik


        
                
______________________________________________________
Click here to donate to the Hurricane Katrina relief effort.
http://store.yahoo.com/redcross-donate3/

--
You receive this message as a subscriber of the xmlc@xxxxxxxxxxxxx mailing list.
To unsubscribe: mailto:xmlc-unsubscribe@xxxxxxxxxxxxx
For general help: mailto:sympa@xxxxxxxxxxxxx?subject=help
ObjectWeb mailing lists service home page: http://www.objectweb.org/wws
Ruby Jobs
Java Jobs
Jobs in California
more...
what
job title, keywords
where
city, state, zip
jobs by job search
Search:
Java, servers, webhosting, windows, cisco ...
more...
<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

Recently Viewed:
encryption.gpg....    ietf.rfc822/199...    freebsd.devel.i...    lang.haskell.li...    mail.squirrelma...    web.zope.plone....    yellowdog.gener...    text.xml.xalan....    recreation.phot...    kde.devel.educa...    hardware.bus.ca...    printing.ghosts...    voip.peering/20...    assembly/2006-0...    org.user-groups...    culture.interne...    network.i2p/200...    boot-loaders.ya...    xfree86.render/...    qnx.openqnx.dev...    jakarta.velocit...    user-groups.pal...   
Home | blog view | USPTO Patent Archive | advertise | OSDir is an inevitable website. super tiny logo

Free Magazines

Cisco News
Receive a free quarterly e-newsletter with exclusive articles on how Cisco IT uses its own products and solutions to enable the business.
subscribe

Systems Management News, the newspaper for IT systems administration and data center managers! Each issue of Systems Management News is chock-full of news and analysis to help you understand what's happening in your field.
subscribe

The Enterprise Newsweekly eWeek is the essential technology information source for builders of e-business.
subscribe

Oracle Magazine Oracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more. Oracle (NASDAQ: ORCL) is the world's largest enterprise software company.
subscribe

Total Telecom Total Telecom is "The Economist of the communications industry".
subscribe