0

Possibly a quite simple question. I have a Element from a XML request and I have done:

Element number;

number = serviceDetail.getChild("Number");

I now want to convert the Element number into a String and then be able to use the substring function on it. What is the most effective way to transform a element into a string? toString() failed and the string prints like @a06816 and trying to transform it seemed to throw alot of errors.

Any help will be appreciated

Edit: Sorry I wasn't 100% clear. The contents of the number element will be the value that is inside the 'Number' element in the XML request which will be a string.

SOLVED: Instead of the above code I used:

String number;
number = serviceDetail.getChildText("Number");

Oops sorry for the hassle guys!

3
  • 1
    What do you want the contents of the string to be? Commented Jul 26, 2011 at 13:40
  • Obtain the content of the element first before trying to convert it to string. What you're doing now is converting the child element (object) to string not the value. Commented Jul 26, 2011 at 13:43
  • possible dup of 632043. See transformer. Commented Jul 26, 2011 at 13:44

2 Answers 2

0

I believe what you are trying to do requires you to marshall the element to your desired destination. If you use JAXB, you can do it like this: (taken from http://ws.apache.org/jaxme/manual/ch02s02.html)

public String asString(JAXBContext pContext, Object pObject) throws JAXBException {

    java.io.StringWriter sw = new StringWriter();

    Marshaller marshaller = pContext.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
    marshaller.marshal(pObject, sw);

    return sw.toString();
}

I -believe- pObject can be an org.w3c.dom.Element (assuming thats the Element class you are using.)

Hope that helps, Nick

Sign up to request clarification or add additional context in comments.

1 Comment

I just noticed the link I posted was for jaxme...oops. But I think the approach for JAXB is identical, or at least similar enough. Getting a JAXBContext however can be a subject unto it's self.
0

Instead of the above code I used:

String number; number = serviceDetail.getChildText("Number");

Oops sorry for the hassle guys!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.