Xml处理——获取Xml格式数据
来源:互联网 发布:rbac java 框架 编辑:程序博客网 时间:2024/05/07 19:36
转换为XML格式
public String asXml(Object obj) throws Exception { JAXBContext context = JAXBContext.newInstance(obj.getClass()); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.setProperty(Marshaller.JAXB_ENCODING, "utf-8"); // 去掉xml头 marshaller.setProperty(Marshaller.JAXB_FRAGMENT, false); StringWriter writer = new StringWriter(); marshaller.marshal(obj, writer); return writer.toString(); }
将XML注入到相应的Bean中
public Object xmlToBean(Class<?> cl, String xml) { JAXBContext jc; try { jc = JAXBContext.newInstance(cl); Unmarshaller unmar = jc.createUnmarshaller(); return unmar.unmarshal(new StringReader(xml)); } catch (JAXBException e) { e.printStackTrace(); } return null; }
获取子元素集合
public Map<String, String> getElementText(Element elt) { Map<String, String> key = new HashMap<String, String>(); for (@SuppressWarnings("rawtypes") Iterator iter = elt.elementIterator(); iter.hasNext();) { Element e = (Element) iter.next(); key.put(e.getName(), e.asXML()); } return key; }
获取根元素
public Element getRootElement(String xml) { Document doc; try { doc = DocumentHelper.parseText(xml); Element rootElt = doc.getRootElement(); return rootElt; } catch (DocumentException e) { e.printStackTrace(); } return null; }
1 0
- Xml处理——获取Xml格式数据
- Android解析数据—XML格式数据
- Xml处理——快速定位Xml节点和获取数据
- 用数据库获取XML格式的数据
- PHP生成和获取XML格式数据
- java获取xml格式字段数据
- PHP生成和获取XML格式数据
- Android:网络:获取XML格式数据
- HttpServletRequest 获取 http 请求xml格式数据
- Ajax获取XML格式的数据
- XMLHttpRequest处理xml格式的返回数据
- Ajax 三级联动,处理xml格式数据
- ajax处理返回的xml格式数据
- xml数据的获取和处理
- xml数据的获取和处理
- php 获取xml接口数据的处理
- C# 获取接口数据(xml格式)转为json格式
- xml格式获取值
- 1.显示和隐藏ActionBar
- Error: Could not create the Java Virtual Machine.
- Mysql学习笔记六,视图
- 项目总结(一) 手机清空后台后,依旧可以接收到推送的解决方案
- IP地址
- Xml处理——获取Xml格式数据
- ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'mysql'
- 通往全栈工程师的捷径 —— react
- Android 设备测试工具
- 前台几个功能使用的框架说明
- Android开发技术之文件的简单存储—小白篇(二)
- 山东理工ACM:1191
- Core Motion
- 凸包水题