logo       

Re: Parsing HTML: msg#00078

java.enhydra.xmlc

Subject: Re: Parsing HTML


Here is an example of creating a Document from a file....of course this example assumes the file is valid XML....

DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance();
dbfactory.setNamespaceAware(true);
DocumentBuilder dbuilder = dbfactory.newDocumentBuilder();
InputStream is = context.getResourceAsStream("/WEB-INF/snippets/header.xml");
Document doc = dbuilder.parse(is);

Note that the "context" is a ServletContext and the way we are loading things makes it so you don't have to provide an absolute file path to the file.  This makes things very portable as there is no extra configuration required and it is platform agnostic.  Plus, it can load the file even if the context is being deployed directly from a .war file where you wouldn't have file IO access within your webapp.

I'm not sure how to modify this example to load a valid HTML4 file?  If you use this example, your file will have to be XHTML valid...meaning that all tags which are opened must then be closed.  If you come up with a way to do this with a non-XML-compliant HTML file, I'd appreciate it if you posted the example back to the list.

later,

Jake

At 12:37 PM 3/24/2003 -0600, you wrote:
That's pretty much what I want to do. I guess my question now is how do I create a document from the file? I know how to compile a document, but I don't know how to load a HTML document into a DOM tree at runtime.

At 12:12 PM 3/24/2003, you wrote:

As long as you get a handle on the <head> node in your current DOM tree, you can either delete that one and import a new node and add it, or I think you can just do it in one-fell-swoop and just use replaceNode().

The place where you import the node from may be from some code snippet file or whatever.  I don't know of any XMLC specific functionality to do this.  I think you'd just be dealing with standard DOM stuff using a DOMLoader to create a document from file.  However, there may very well be some utility methods to do this via XMLC that I'm not aware of.

--
Ben Sinclair
ben@xxxxxxxxxxxxxxx
_______________________________________________
XMLC mailing list
XMLC@xxxxxxxxxxx
http://www.enhydra.org/mailman/listinfo.cgi/xmlc
<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise