JAXB解析xml文件转换为POJO
Marshal 和 UnMarshal的过程并不复杂,只需要从JAXBContext中获得Marshaller或Unmarshaller对象,就可以让JAXB帮我们 来进行转换。 1.将 xml 文件中的各个节点和属性信息创建对应的Java模型
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<Rule> rules; @XmlElementWrapper(name = "actions") @XmlElement(name = "action") private List<Action> actions; public List<Rule> getRules() { return rules; } public String getServiceName() { return serviceName; } public void setServiceName(String serviceName) { this.serviceName = serviceName; } public void setRules(List<Rule> rules) { this.rules = rules; } public List<Action> getActions() { return actions; } public void setActions(List<Action> 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<Object> list = new ArrayList<Object>(); for (int i = 0; i < fs.length; i++) { InputStream input = new FileInputStream(fs[i]); JavaBean bean = (JavaBean) XMLMarshallUtil.unmarshal(input,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<JavaBean> list = (List<JavaBean>) obj; for (JavaBean bean : list) { System.out.println(bean.getActions().get(1).getActionName()); } } }
1)解析文件中JAXBContext.newInstance(Service.class)创建新的实例错误,正确的是 (ServiceMetric.class) javax.xml.bind.UnmarshalException - with linked exception: [java.io.IOException: Stream closed] 2)使用 StreamSource 类出现的异常 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.] 3)解析过程中返回的对象中有引用类型的属性,需要将里面所有的引用类型属性,全都实例化(ObjectFactory作用)。 Exception in thread "main" javax.xml.bind.JAXBException: "bean.javaBean" doesnt contain ObjectFactory.class or jaxb.index 本文转载自http://hanhongke123.blog.163.com/blog/static/622234942012443279559/ (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |