|
BTW if you're working with XPath in JSP you might
find the XML tags in JSTL useful
Anyways, you can use XPath to extract the
attributes.
List nodes = doc.selectNodes( "//foo"
);
XPath xpath = doc.createXPath( "@x" );
List attributes = xpath.selectNodes( nodes
);
Or you can iterate through the List (which is a
java.util.List) and cast each node to an Element and use the
element.attribute("x") method to extract the Attribute object, or use
element.attributeValue("x") to extract the attribute value.
James
----- Original Message -----
Sent: Friday, April 26, 2002 9:00
PM
Subject: Re: [dom4j-user] I just want a
DOMDocument
Thanks James,
I've kind of got a better grasp of what I want to
do with the DOMDocument (i'm used to that MSXML thing). I've gotten as
far as returning a List from a document.selectNodes("my_xpath"). I need
to get at the attributes for each Node in the list. I would have thought
I could just do a ((Node) list.get(0)).getAttributes(), but the getAttributes
method is only defined in the DOMDocument object. I can't obviously cast
my list.get() to a DOMDocument, but I'd like a NamedNodeMap containing the
attributes of my selected node.
I'm running this code in a JSP page, using Tomcat
3.2.3.
For those interested, here's the code I'm using
(abridged):
<%@ page import ... (all required libraries
and classes)%>
<%
SAXReader reader = new
SAXReader(); reader.setDocumentFactory(new
DOMDocumentFactory()); Document document =
reader.read("url_path_to_my_xml");
List layerNodes =
document.selectNodes("//LAYER");
%>
Now how do I get the attributes for each node in the list?
Thanks in advance.
----- Original Message -----
Sent: Friday, April 26, 2002 11:30
AM
Subject: Re: [dom4j-user] I just want a
DOMDocument
You can try use the DOMWriter which will take
any dom4j document and create a W3C DOM implementation, using any JAXP W3C
DOM provider.
There's also DOMDocumentFactory that
you can use and set on the SAXReader via the setDocumentFactory() method and
everything will just work.
James
----- Original Message -----
Sent: Friday, April 26, 2002 7:27
PM
Subject: [dom4j-user] I just want a
DOMDocument
This is probably a painfully simple question,
but bear with me. :)
I'm starting to use dom4j, and I want to
parse an xml file, returning a org.dom4j.dom.DOMDocument object.
From the javadocs and the quickstart guide, I can easily see how to return
a org.dom4j.Document object, but I need the functionality of the
DOMDocument. I know that the DOMDocument implements the Document
interface - can I just cast the returned Document to a DOMDocument?
Is it already returning a DOMDocument and I just don't know? I feel
pretty stupid here, 'cause I couldn't find the answer in the old posts
either.
Thanks in advance.
John
Fletcher
|