Parse XML documents

M

Martin

Hello,
I've tried to parse some XML document with the help of a tutorial
(http://java.sun.com/webservices/docs/1.0/tutorial/doc/JAXPDOM2.html).
I think I understand the whole thing the documents, but something
isn't working:
<FOO attr1="234" attr2="423"/>
In this example FOO is an element which is parsed and displayed
properly, but attr1 and attr2 are never shown in the tree. I don't
know how to access these attributes. Is it possible at all or what
do you suggest?

Ciao,
Martin
 
M

Murray

Martin said:
Hello,
I've tried to parse some XML document with the help of a tutorial
(http://java.sun.com/webservices/docs/1.0/tutorial/doc/JAXPDOM2.html).
I think I understand the whole thing the documents, but something
isn't working:
<FOO attr1="234" attr2="423"/>
In this example FOO is an element which is parsed and displayed
properly, but attr1 and attr2 are never shown in the tree. I don't
know how to access these attributes. Is it possible at all or what
do you suggest?

Ciao,
Martin

I don't know exactly what you mean by "never shown in the tree". How exactly
are you "showing" the tree? Do you already have a Document/Element object?
If so you can access it's attributes using Element#getAttribute(String)
 
M

Martin

Murray said:
I don't know exactly what you mean by "never shown in the tree".
How exactly are you "showing" the tree? Do you already have a
Document/Element object? If so you can access it's attributes
using Element#getAttribute(String)
This helped me a lot, thank you. Now I can access and print the
attributes, but how can I place them in my JTree?
I actually took this code
http://java.sun.com/xml/jaxp/dist/1.1/docs/tutorial/dom/work/DomEcho06.java
and modified it, so I can edit the values in the leafs. I think I
have to modify the toString() and content() in the AdapterNode
Class, so the attribute name is displayed as a Node in the JTree
and the attribute's value is displayed in the TextPane? Or how can
I place these attributes as nodes in the tree?

Ciao,
Martin
 
J

Johan Poppe

Martin skrev:
I think I
have to modify the toString() and content() in the AdapterNode
Class, so the attribute name is displayed as a Node in the JTree
and the attribute's value is displayed in the TextPane? Or how can
I place these attributes as nodes in the tree?

You can show whatever you want, including (but not limited to) both
XML attributes and pictures of your grandmother, as nodes in a JTree.

If you choose to show the attributes as separate nodes or somehow more
linked to the node representing , is entirely up to you. (And possibly
your boss, but that's another issue.)
 
J

Jim

Hello,
I've tried to parse some XML document with the help of a tutorial
(http://java.sun.com/webservices/docs/1.0/tutorial/doc/JAXPDOM2.html).
I think I understand the whole thing the documents, but something
isn't working:
<FOO attr1="234" attr2="423"/>
In this example FOO is an element which is parsed and displayed
properly, but attr1 and attr2 are never shown in the tree. I don't
know how to access these attributes. Is it possible at all or what
do you suggest?

Ciao,
Martin

When you are examining a Node you need to call the getAttributes()
method to get a Collection of the attributes for the node.

Jim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top