Get Data and Value of XML element in Java

V

vunet.us

I am new to Java. I need to rewrite a function which gets XML elements
text as shown below to get elements value instead:

private String getCharacterDataFromElement(Element e){
Node child = e.getFirstChild();
if(child instanceof CharacterData){
CharacterData cd = (Character) child;
return cd.getData();
}
return "?";
}

<xml>FUNCTION ABOVE GETS THIS<xml>

<xml value="NEED TO GET THIS VALUE"></xml>

THANKS TO ALL.
 
O

Oliver Wong

I am new to Java. I need to rewrite a function which gets XML elements
text as shown below to get elements value instead:

private String getCharacterDataFromElement(Element e){
Node child = e.getFirstChild();
if(child instanceof CharacterData){
CharacterData cd = (Character) child;
return cd.getData();
}
return "?";
}

<xml>FUNCTION ABOVE GETS THIS<xml>

<xml value="NEED TO GET THIS VALUE"></xml>

THANKS TO ALL.

http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/text/Element.html#getAttributes()

- Oliver
 
A

Andrew Thompson

I am new to Java.
Wow!

...I need to rewrite a function which gets XML elements
text as shown below to get elements value instead:

Cool! When you finish, could you send me the
codes too?

Andrew T.
 
S

Steve W. Jackson

I am new to Java. I need to rewrite a function which gets XML elements
text as shown below to get elements value instead:

private String getCharacterDataFromElement(Element e){
Node child = e.getFirstChild();
if(child instanceof CharacterData){
CharacterData cd = (Character) child;
return cd.getData();
}
return "?";
}

<xml>FUNCTION ABOVE GETS THIS<xml>

<xml value="NEED TO GET THIS VALUE"></xml>

THANKS TO ALL.

You should become familiar with the API Javadocs, as you'll find a great
part of what you want to know in there. For this question, start with
<http://java.sun.com/j2se/1.5.0/docs/api/org/w3c/dom/Node.html>.

Assuming that the parameter "e" is the element in your (incorrect)
examples named "xml", you can get the value of the attribute named
"value" using:

String value = e.getAttribute("value");

If the attribute doesn't exist or is an empty string, you'll get an
empty string in return.

To get the value of the text node, you could try this:

if (e.getFirstChild().getNodeType() == Node.TEXT_NODE) {
String nodevalue = e.getFirstChild().getNodeValue();
}

= Steve =
 
V

vunet.us

Thank you Oliver, but I saw it many times. The problem is: I am new to
Java... I cannot understand how to use it.
Andrew, I will send you the code to your personal email... What is it?
Thanks.
 
O

Oliver Wong

Thank you Oliver, but I saw it many times. The problem is: I am new to
Java... I cannot understand how to use it.

Then I recommend you learn Java. Otherwise, you'll have to keep coming
back here every step of your development process. If you have a specific
question about Java, we can try to answer it. Otherwise, I can only
recommend that you google for "Java tutorial" and follow the tutorials you
find.

- Oliver
 
V

vunet.us

Oliver,
Your recommendations are stronly considered. By the way, I was always
wondering, what is the reason of being helped on Google Groups? Is it
because some people want to really help others, or there may be
something behind this? I really wonder. Or maybe I have to do something
in return. Please, help me know. Thank you.
 
O

Oliver Wong

Oliver,
Your recommendations are stronly considered. By the way, I was always
wondering, what is the reason of being helped on Google Groups? Is it
because some people want to really help others, or there may be
something behind this? I really wonder. Or maybe I have to do something
in return. Please, help me know. Thank you.

I don't know. You'd probably have to ask a psychology newsgroup for that
one.

- Oliver
 
D

Dag Sunde

Oliver,
Your recommendations are stronly considered. By the way, I was always
wondering, what is the reason of being helped on Google Groups? Is it
because some people want to really help others, or there may be
something behind this? I really wonder. Or maybe I have to do
something in return. Please, help me know. Thank you.

Two answers here...

You're not being helped on "Google Groups"! "Google Groups" is just
a web-site that let you view and post to UseNet newsgroups. Most
experienced people won't even browse into "GG", but use a dedicated
News Client, which is *made* for participating on Usenet.

Second, it is because some people want to really help others.
And because those who help others also learn from the discussions
themselves.
 

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,992
Messages
2,570,220
Members
46,807
Latest member
ryef

Latest Threads

Top