栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

文件中未编组对象的JAXB位置

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

文件中未编组对象的JAXB位置

您可以在JAXB中利用an

XMLStreamReader
和an 来做到这一点
Unmarshaller.Listener

演示版

package forum383861;import java.io.FileInputStream;import java.util.HashMap;import java.util.Map;import javax.xml.bind.JAXBContext;import javax.xml.bind.Unmarshaller;import javax.xml.bind.Unmarshaller.Listener;import javax.xml.stream.Location;import javax.xml.stream.XMLInputFactory;import javax.xml.stream.XMLStreamReader;public class Demo {    public static void main(String[] args) throws Exception {        JAXBContext jc = JAXBContext.newInstance(Customer.class);        XMLInputFactory xif = XMLInputFactory.newFactory();        FileInputStream xml = new FileInputStream("src/forum383861/input.xml");        XMLStreamReader xsr = xif.createXMLStreamReader(xml);        Unmarshaller unmarshaller = jc.createUnmarshaller();        LocationListener ll = new LocationListener(xsr);        unmarshaller.setListener(ll);        Customer customer = (Customer) unmarshaller.unmarshal(xsr);        System.out.println(ll.getLocation(customer));        System.out.println(ll.getLocation(customer.getAddress()));    }    private static class LocationListener extends Listener {        private XMLStreamReader xsr;        private Map<Object, Location> locations;        public LocationListener(XMLStreamReader xsr) { this.xsr = xsr; this.locations = new HashMap<Object, Location>();        }        @Override        public void beforeUnmarshal(Object target, Object parent) { locations.put(target, xsr.getLocation());        }        public Location getLocation(Object o) { return locations.get(o);        }    }}

input.xml

<?xml version="1.0" encoding="UTF-8"?><customer>    <address/></customer>

输出量

[row,col {unknown-source}]: [2,1][row,col {unknown-source}]: [3,5]

顾客

package forum383861;import javax.xml.bind.annotation.XmlRootElement;@XmlRootElementpublic class Customer {    private Address address;    public Address getAddress() {        return address;    }    public void setAddress(Address address) {        this.address = address;    }}

地址

package forum383861;public class Address {}

想要查询更多的信息

  • http://blog.bdoughan.com/2011/08/using-unmarshallerlistener-to-capture.html


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/509686.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号