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: John Cowan <jcowan AT reutershealth.com>
  • To: Arjan Huijzer <huijzer AT gmail.com>
  • Cc: xom-interest AT lists.ibiblio.org
  • Subject: Re: [XOM-interest] CDATA sections
  • Date: Wed, 5 Jan 2005 14:18:35 -0500

Arjan Huijzer scripsit:
> John,
>
> I am almost there, just need to get rid of the namespace declarations.
> Can this be done programmatically?

Absolutely. See below.

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

That only works when you are calling the main method.

> // 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");

tagsoup.setFeature("http://xml.org/sax/features/namespaces";,
false);

tagsoup.setFeature(""http://xml.org/sax/features/namespace-prefixes";, false);

> Builder bob = new Builder(tagsoup);
> Document doc = bob.build(new StringReader(unparsedContent));
>
> // Get to the <body> element
> Node bodyNode = doc.getRootElement().getChild(0);

This isn't reliable; if the user supplies a title element, then getChild(0)
will be the head element generated by TagSoup.

> 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>
>
>
>
--
But you, Wormtongue, you have done what you could for your true master. Some
reward you have earned at least. Yet Saruman is apt to overlook his bargains.
I should advise you to go quickly and remind him, lest he forget your faithful
service. --Gandalf John Cowan <jcowan AT reutershealth.com>




Archive powered by MHonArc 2.6.24.

Top of Page