开发者

How can I convert XML namespace into a Java package name easily?

开发者 https://www.devze.com 2023-02-11 09:18 出处:网络
I need to convert XML namespace into a Java package name in the same way as JAXB or XmlBeans. For example, http:\\www.widgetvendor.com\\types\\widgetTypes.xsd should be converted to com.widgetvendor.

I need to convert XML namespace into a Java package name in the same way as JAXB or XmlBeans.

For example, http:\www.widgetvendor.com\types\widgetTypes.xsd should be converted to com.widgetvendor.types.wi开发者_如何转开发dgettypes

I can code my own function to perform this conversion, but I think that it is common task and there should be utilities.

Can you name any?


Yes, there is an easy way to convert an XML namespace to a Java package name!

You can use the same class that is internally used by JAXB to perform the operation, as per the JAXB Specification 2.0 Final Release (§D.5.1 Generating a Java package name : Mapping from a Namespace URI) conversion rules.

I came across this posting from 2011 while searching for a standard utility class that would perform this operation. Unfortunately, snarky responses do not help anyone. Just in case the next person comes across this question and would like an actual answer please see the code snippet below:

/* Also available in the internal package space as part of the JDK */
import com.sun.xml.bind.api.impl.NameConverter; 

public static String convertToPackageName(String xmlNamespace) {
    NameConverter nameConverter = new NameConverter.Standard();
    return nameConverter.toPackageName(xmlNamespace);
}


this link might help you http://forums.java.net/node/690286

but it is better to write your own function where you can customize if there is some requirement in the future.


This is not a common task. You are best off coding the function yourself.

(And before you ask ... No, I won't do it for you. Sorry.)

0

精彩评论

暂无评论...
验证码 换一张
取 消