Skip to Content.
Sympa Menu

xom-interest - Re: [XOM-interest] CDATA sections

xom-interest AT lists.ibiblio.org

Subject: XOM API for Processing XML with Java

List archive

Chronological Thread  
  • From: Arjan Huijzer <huijzer AT gmail.com>
  • To: xom-interest AT lists.ibiblio.org
  • Subject: Re: [XOM-interest] CDATA sections
  • Date: Wed, 5 Jan 2005 16:44:58 +0100

John,

I am almost there, just need to get rid of the namespace declarations.
Can this be done programmatically?

Thanks,
Arjan

=================================

import java.io.StringReader;

import nu.xom.Builder;
import nu.xom.Document;
import nu.xom.Element;
import nu.xom.Node;

import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;

public class TagSoupTest {

public static void main(String[] args) throws Exception {

// This does not seem to work .... what did I do wrong ....
System.setProperty("nons", "true");

// Some raw HTML content supplied by a user
String unparsedContent = "<h1>title</H1>";

// Create a <content> element to hold supplied content
Element content1 = new Element("content");
Element content2 = new Element("content");


// Create a well-formed XML document based on the unparsed
HTML
XMLReader tagsoup = XMLReaderFactory

.createXMLReader("org.ccil.cowan.tagsoup.Parser");
Builder bob = new Builder(tagsoup);
Document doc = bob.build(new StringReader(unparsedContent));

// Get to the <body> element
Node bodyNode = doc.getRootElement().getChild(0);

int childCount = bodyNode.getChildCount();
for (int i=0; i<childCount; i++) {
Node child = bodyNode.getChild(i);
content1.appendChild( child.copy() );
content2.appendChild( child.toXML() );
}

// Check was has been generated
System.out.println( content1.toXML() );
System.out.println( content2.toXML() );
}
}


This prints out:

<content><h1 xmlns="http://www.w3.org/1999/xhtml";>title</h1></content>
<content>&lt;h1&gt;title&lt;/h1&gt;</content>


But what I would like to get is:

<content><h1>title</h1></content>



On Wed, 5 Jan 2005 10:19:28 -0500, John Cowan <jcowan AT reutershealth.com>
wrote:
> Arjan Huijzer scripsit:
>
> > I am now trying to use your TagSoup parser, but I am running into some
> > trouble, because TagSoup creates an entire xhtml document. But,
> > instead, I just want an XML fragment containing the uploaded content.
> > Is there a way to easily achieve that?
>
> You probably don't want to use Serializer anyhow, or at least not
> with the given options, because whitespace around element content is
> significant in HTML. So navigate down to the body element generated by
> TagSoup and use toXML() on its children. (If there isn't a body element,
> the document is degenerate and you don't want to use it anyway.)
>
> --
> Real FORTRAN programmers can program FORTRAN John Cowan
> in any language. --Allen Brown jcowan AT reutershealth.com
>




Archive powered by MHonArc 2.6.24.

Top of Page