Skip to main content

How to parse xml:lang?

5 replies [Last post]
mnroder
Offline
Joined: 2005-07-18
Points: 0

How do I set the annotations on a POJO to deal with xml:lang attributes and also to properly generate the schema using the schemagen ant task?

Example:

aVal

Some documentation

Thanks

Mark

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
kohsuke
Offline
Joined: 2003-06-09
Points: 0

[code]
@XmlRootElement
class SomeObject {
@XmlElement
String someVal;
@XmlElement
Documentation documentation;
}

class Documentation {
@XmlAttribute(name="lang",namespace="...")
String lang;

@XmlValue
String text;
}
[/code]

mnroder
Offline
Joined: 2005-07-18
Points: 0

I still can not get it to work. If I have namespace="xml", it seems to parse and not get a validation exception, but it never sets the value.

If I change the xml content to have foo:lang instead of xml:lang as the attribute, I get an error with a namespace is not bound as expected in my xml fragement.

Is the parser handling this different since it is a part of the standard xml standard( reference: http://www.w3.org/XML/1998/namespace and section 2.12 of http://www.w3.org/TR/xml11/)

Once I can get it to parse properly, how do I get it to generate the proper namespace definition, import and ref to support it in a generated xsd file? (reference: http://www.xml.com/pub/a/2000/11/29/schemas/part1.html?page=8)

Thanks in advance.

Mark

Here is a test case to validate the parsing:

public class LangTest extends TestCase {

public void testSimpleReadWorks() throws Exception{
String validXml =
"" +
"oneVal" +
"two" +
"
";

SomeObject someObject = (SomeObject) parseString(validXml);
assertNotNull(someObject);
assertEquals("oneVal", someObject.valOne);
assertEquals("en", someObject.documentation.lang);
assertEquals("two", someObject.documentation.value);
}

private Object parseString(String xmlString) throws Exception {
JAXBContext jc = JAXBContext.newInstance(SomeObject.class);
Unmarshaller u = jc.createUnmarshaller();
ValidationEventHandler handler = new ValidationEventHandler(){
public boolean handleEvent(ValidationEvent event) {
return false;
}
};
u.setEventHandler(handler);

StringReader reader = new StringReader(xmlString);
Object obj = u.unmarshal(reader);
return obj;
}
}

kohsuke
Offline
Joined: 2003-06-09
Points: 0

Note that it's

@XmlAttribute(name="lang",namespace="http://www.w3.org/XML/1998/namespace")

and not

@XmlAttribute(name="lang",namespace="xml")

because 'xml' is just a prefix.

mnroder
Offline
Joined: 2005-07-18
Points: 0

Thanks. I can now parse properly.

When I generate the schema from java using the schemagen ant task, how do I have it generate the namespace def and import?

The generated file does not contain xmlns:xml or the import:
Gen file just has:
xmlns:xs="http://www.w3.org/2001/XMLSchema">
...

The working xsd file with hand edit:
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xml="http://www.w3.org/XML/1998/namespace">

schemaLocation="http://www.w3.org/2001/xml.xsd"/>













Thanks

Mark

kohsuke
Offline
Joined: 2003-06-09
Points: 0

You don't need the xmlns:xml declaration. I believe a parser that accepts such namespace declaration is in violation of the XML namespace spec.

We need to enhance the schema generator tool to generate the following import statement:

[code]
namespace="http://www.w3.org/XML/1998/namespace"
schemaLocation="http://www.w3.org/2001/xml.xsd"/>
[/code]

... or perhaps just document it --- I thought we've done something in this area already.

If you can file an issue on https://jaxb.dev.java.net/ about this, that would be great.