@H_404_1@namespace 指定Schema中的命名空间
@XmlElementWrapper 为数组元素或集合元素定义一个父节点。
如,类中有一元素为List items,若不加此注解,该元素将被映射为
...
...
这种形式,此注解可将这个元素进行包装,如:
@XmlElementWrapper(name="items")
@XmlElement(name="item")
public List items;
将会生成这样的XML样式:
...
...
@XmlJavaTypeAdapter 自定义某一字段或属性映射到XML的适配器。
如,类中包含一个接口,我们可以定义一个适配器(继承自 javax.xml.bind.annotation.adapters.XmlAdapter类),指定这个接口如何映射到XML。
@H_404_1@
@XmlSchema 配置整个包的namespace,这个注解需放在package-info.java文件中。@H_404_1@
@H_404_1@
3.示例代码
//Java Entity
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "automation")
public class JavaBean
{
@XmlAttribute(name = "service")
private String serviceName;
@XmlElementWrapper(name = "rules")
@XmlElement(name = "rule")
private List rules;
@XmlElementWrapper(name = "actions")
@XmlElement(name = "action")
private List actions;
public List getRules()
{
return rules;
}
public String getServiceName()
{
return serviceName;
}
public void setServiceName(String serviceName)
{
this.serviceName = serviceName;
}
public void setRules(List rules)
{
this.rules = rules;
}
public List getActions()
{
return actions;
}
public void setActions(List actions)
{
this.actions = actions;
}
}
//ObjectFactory
@XmlRegistry
public class ObjectFactory
{
private final static QName _Configurations_SCALERULE_QNAME = new QName(
"http://www.huawei.com/cloudCube/cloudview","entities");
public ObjectFactory()
{
}
public Action createAction()
{
return new Action();
}
public ActionProperty createActionProperty()
{
return new ActionProperty();
}
public JavaBean createJavaBean()
{
return new JavaBean();
}
public Dimension createDimension()
{
return new Dimension();
}
public Rule createRule()
{
return new Rule();
}
}
//解析
public static Object unmarshal(InputStream input,String classPath,
String schemaResPath,ClassLoader classLoader)
throws JAXBException,SAXException
{
JAXBContext jaxbContext = JAXBContext.newInstance(classPath,
classLoader);
SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
if (schemaResPath != null && !schemaResPath.isEmpty())
{
URL schemaURL = classLoader.getResource(schemaResPath);
Schema schema = schemaFactory.newSchema(schemaURL);
unmarshaller.setSchema(schema);
}
Object obj = unmarshaller.unmarshal(input);
return obj;
}
//转换
public static Object convertor(String fi) throws Exception
{
ClassLoader classLoader = ObjectFactory.class.getClassLoader();
String contextPath = ObjectFactory.class.getPackage().getName();
File file = new File(fi);
if (!file.isDirectory())
{
InputStream input = new FileInputStream(file);
JavaBean bean = (JavaBean) XMLMarshallUtil.unmarshal(input,
contextPath,
null,
classLoader);
return bean;
}
else
{
File[] fs = file.listFiles();
List list = new ArrayList();
for (int i = 0; i < fs.length; i++)
{
InputStream input = new FileInputStream(fs[i]);
JavaBean bean = (JavaBean) XMLMarshallUtil.unmarshal(input,
contextPath,
null,
classLoader);
list.add(bean);
}
return list;
}
}
//main函数
public static void main(String[] args) throws Exception
{
Object obj = Test.convertor("bin/conf/");
if (obj instanceof JavaBean)
{
JavaBean bean = (JavaBean) obj;
System.out.println(bean.getServiceName());
}
if (obj instanceof List>)
{
List list = (List) obj;
for (JavaBean bean : list)
{
System.out.println(bean.getActions().get(1).getActionName());
}
}
}
@H_404_1@
4.解析过程中出现异常
1)解析文件中JAXBContext.newInstance(Service.class)创建新的实例错误,正确的是 (ServiceMetric.class)
javax.xml.bind.UnmarshalException
- with linked exception:
[java.io.IOException: Stream closed]
@H_404_1@
@H_404_1@
2)使用 StreamSource 类出现的异常@H_404_1@
StreamSource streamSource = new StreamSource(read);
root = (Service) um.unmarshal(streamSource);
javax.xml.bind.UnmarshalException
- with linked exception:
[org.xml.sax.SAXParseException: Premature end of file.]@H_404_1@
3)解析过程中返回的对象中有引用类型的属性,需要将里面所有的引用类型属性,全都实例化(ObjectFactory作用)。@H_404_1@
Exception in thread "main" javax.xml.bind.JAXBException: "bean.javaBean" doesnt contain ObjectFactory.class or jaxb.index@H_404_1@
总结
如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。