[jdom-interest] Embedding HTML in XML
James Davies
J.Davies at jacobus.co.uk
Mon Jun 26 14:13:59 PDT 2000
Hi Richard,
There /are/ some HTML parsers out there which might do what you want.
I repeated an earlier reference to one such in my 'TreeOutputter' post,
repeated below:
HTH?
Jim
Posted 17-May-00:
Hi All,
For what it's worth, enclosed is a very simple JDOM Outputter that creates a
(sub)tree in a JTree. Not yet javadoc'd.
Public methods are:
public boolean getCaptionAttributes()
public void outputToTree(Document doc, DefaultMutableTreeNode parent)
public void setCaptionAttributes(boolean value)
The 'outputToTree' method takes a parent node, rather than a JTree
reference, so you can insert different documents wherever you like in a
tree.
The 'CaptionAttributes' property tells it whether to (true) output Element
Attributes after the Element name, as the caption of the Element node, or
(false) create a child node captioned 'Attributes' for each Element that has
Attributes, with child nodes for each Attribute value.
Hope this is useful to others - I find it good for experimenting with
different parsers and test data.
If you're using an IDE like JBuilder, you can just plonk a JTree on a form
(hence object 'jTree1' below).
Example calling code, including three different XML sources:
void Test() {
Document doc;
// Get the root node of 'jTree1'.
DefaultTreeModel model = (DefaultTreeModel)jTree1.getModel();
DefaultMutableTreeNode root = (DefaultMutableTreeNode)model.getRoot();
// Create a TreeOutputter.
TreeOutputter outputter = new TreeOutputter();
// Create a JDOM document from an XML text file.
try {
SAXBuilder builder = new
SAXBuilder("org.apache.xerces.parsers.SAXParser");
doc = builder.build(new File("c:\\XML Data\\test.xml"));
} catch (JDOMException je) {
return;
}
// Add to the tree root.
outputter.outputToTree(doc, root);
// Create a JDOM document from a SQL table.
try {
SQLBuilder builder = new SQLBuilder("jdbc:odbc:MS Access Database");
builder.setAsAttributes("u_code");
// builder.setAsAttributes("u_code,u_name");
builder.setRootName("Users");
builder.setEntryName("User");
doc = builder.build("select * from Users");
} catch (JDOMException je) {
return;
}
// Add to the tree root.
outputter.outputToTree(doc, root);
// Create a JDOM document from an HTML file.
System.out.println("DBrownell's HtmlParser...");
try {
SAXBuilder builder = new SAXBuilder("org.brownell.xml.HtmlParser");
doc = builder.build(new File("c:\\HTML Data\\test.htm"));
} catch (Exception ex) {
System.out.println("Exception: " + ex.getMessage());
return;
}
// Add to the tree root.
outputter.outputToTree(doc, root);
// Reload the tree model.
model.reload();
}
References
----------------
- Jon Baer's very useful SQLBuilder:
http://www.digitalanywhere.com/projects/sqlbuilder/
- David Brownell's HTML parser. I've found this great at parsing many
random Web HTML pages, although I've hit stack errors with some. It's
available from:
http://home.pacbell.net/david-b/xml/ (thanks to Simon St.Laurent for this
reference).
Jim
-----------------------------------------------------------------
Dr James G. Davies
Jacobus Systems Limited, London UK
----- Original Message -----
From: "Richard Waters" <rwaters at marimba.com>
To: <jdom-interest at jdom.org>
Sent: Monday, June 26, 2000 7:14 PM
Subject: [jdom-interest] Embedding HTML in XML
> hi all,
>
> i'm trying out a little servlet project with XML - i was previously using
> the xerces api, but things have become so much easier after switching to
> jdom. great job!
>
> so maybe someone can help me with this problem: i have users submitting
html
> forms, the content of which is converted into some xml (with
XMLOutputter),
> and later rendered as html (using xalan). i'd like users to be able to
enter
> some html in those textboxes, and have that correctly rendered later.
>
> the only way i came up with to do this is to parse the text submitted with
> the html forms, create xml elements for the html elements i find, generate
a
> list with these elements and the text, and then use setMixedContent().
> that's a pretty painful way to do it. since i'm pretty much a novice so
far,
> i'm hoping i'm just failing to see a much easier way to do this.
>
> thanks a lot for any advice you can give,
> rick
> _______________________________________________
> To control your jdom-interest membership:
>
http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhos
t.com
More information about the jdom-interest
mailing list