0

Has anyone ever done any type of XML paring with elements that have dashes in the names? I don't think its possible, any ideas?

<Person-Response>
   <First-Name> 3119043033121014002</First-Name>
   <Last-Name> 3119043033121014002</Last-Name>
</Person-Response>

5 Answers 5

3

If this is a questin regarding XML to java mapping then JAXB can handle it:

@XmlRootElement(name = "Person-Response")
public class PersonResponse {
    @XmlElement(name = "First-Name")
    String firstName;
    @XmlElement(name = "Last-Name")
    String lastName;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Hi Norrman, I'm getting an xml string and I need to turn it into the entity you outlined above. I didn't know of a way with JAXB to convert from an XML String to an Object without a schema. Do you have an example to do as much? Thanks for the help, much appreciated!
Study the documentation of javax.xml.bind.JAXBContext. You can create a JAXBContext with reference to a set of annotated classes, and then create an Unmarshaller from that context.
3

Regarding the dashes, see the official XML recommendation of the W3C:

Document authors are encouraged to use names which are meaningful words or combinations of words in natural languages, and to avoid symbolic or white space characters in names. Note that COLON, HYPHEN-MINUS, FULL STOP (period), LOW LINE (underscore), and MIDDLE DOT are explicitly permitted.

Comments

2

This is legal XML; any compliant parser should have no problems with it.

1 Comment

It is, in fact, not legal. Note the trailing 't' in the closing tag, which might be the source of the problem.
0

There should not be any problem because of the dashes. But it is an invalid xml as per the reason mentioned by Per Norman

1 Comment

how would you parse something like this to a java object and get the values out?
0

It is a legal XML and any parser could parse it.

Regarding Java, you have the JAXP API support for parsing this XML in either DOM, SAX or StAX.

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.