本文介绍了使用jaxb进行解组时各个XML元素的行号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有一个具有属性名称和地址的Person。我用XML显示它。从XML解组时,可以分别获取名称和地址的行号。 我尝试使用Locator。但它没有提供单独的行号。解决方案 EclipseLink JAXB(MOXy) 和JAXB参考实现都有自己的 @XmlLocation 支持此用例的注释。这允许您将对应于该对象的XML元素上的位置存储为 org.xml.sax.Locator 的实例。由于我是MOXy领导者,我将演示使用MOXy: 人 import javax.xml.bind.annotation。*; import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; import org.eclipse.persistence.oxm.annotations.XmlLocation; import org.xml.sax.Locator; @XmlRootElement @XmlAccessorType(XmlAccessType.FIELD) public class Person { @XmlJavaTypeAdapter(value = StringAdapter.class)字符串名称; 地址; @XmlLocation 定位器定位器; } 地址 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; import org.eclipse.persistence.oxm.annotations.XmlLocation; import org.xml.sax.Locator; 公共类地址{ @XmlJavaTypeAdapter(value = StringAdapter.class) private String street; @XmlLocation 定位器定位器; } StringAdapter import javax.xml.bind.annotation。*; import javax.xml.bind.annotation.adapters.XmlAdapter; import org.eclipse.persistence.oxm.annotations.XmlLocation; import org.xml.sax.Locator; public class StringAdapter extends XmlAdapter< StringAdapter.AdaptedString,String> { public static class AdaptedString { @XmlValue public String value; @XmlLocation @XmlTransient 定位器定位器; } @Override public String unmarshal(AdaptedString v)抛出异常{ System.out.println(v.value ++ v.locator.getLineNumber()); 返回v.value; } @Override public AdaptedString marshal(String v)throws Exception { AdaptedString adaptedString = new AdaptedString(); adaptString.value = v; 返回adaptationString; } } jaxb.properties 要将MOXy指定为JAXB提供程序,您需要在其中包含一个名为 jaxb.properties 的文件与您的域模型相同的包,包含以下条目。 javax.xml.bind.context.factory = org.eclipse.persistence .jaxb.JAXBContextFactory 演示 import java.io.File; import javax.xml.bind。*; 公共类演示{ public static void main(String [] args)throws Exception { JAXBContext jc = JAXBContext.newInstance(Person.class); Unmarshaller unmarshaller = jc.createUnmarshaller(); 文件xml =新文件(src / forum14455596 / input.xml); 人格=(人)unmarshaller.unmarshal(xml); System.out.println(Person:+ person.locator.getLineNumber()); System.out.println(Address:+ person.address.locator.getLineNumber()); } } 输出 Jane Doe 3 1 A Street 5 人:2 地址:4 I have a class Person with attributes name and address. I display it in a XML. While unmarshalling from XML will it be possible to get line number for name and address separately.I tried using Locator. But it does not provide individual line numbers. 解决方案 The EclipseLink JAXB (MOXy) and the JAXB reference implementation each have their own @XmlLocation annotations for supporting this use case. This allows you to store the location on the XML element corresponding to the object as an instance of org.xml.sax.Locator. Since I'm the MOXy lead, I will demonstrate using MOXy:Personimport javax.xml.bind.annotation.*;import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;import org.eclipse.persistence.oxm.annotations.XmlLocation;import org.xml.sax.Locator;@XmlRootElement@XmlAccessorType(XmlAccessType.FIELD)public class Person { @XmlJavaTypeAdapter(value=StringAdapter.class) String name; Address address; @XmlLocation Locator locator;}Addressimport javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;import org.eclipse.persistence.oxm.annotations.XmlLocation;import org.xml.sax.Locator;public class Address { @XmlJavaTypeAdapter(value=StringAdapter.class) private String street; @XmlLocation Locator locator;}StringAdapterimport javax.xml.bind.annotation.*;import javax.xml.bind.annotation.adapters.XmlAdapter;import org.eclipse.persistence.oxm.annotations.XmlLocation;import org.xml.sax.Locator;public class StringAdapter extends XmlAdapter<StringAdapter.AdaptedString, String> { public static class AdaptedString { @XmlValue public String value; @XmlLocation @XmlTransient Locator locator; } @Override public String unmarshal(AdaptedString v) throws Exception { System.out.println(v.value + " " + v.locator.getLineNumber()); return v.value; } @Override public AdaptedString marshal(String v) throws Exception { AdaptedString adaptedString = new AdaptedString(); adaptedString.value = v; return adaptedString; }}jaxb.propertiesTo specify MOXy as your JAXB provider you need to include a file called jaxb.properties in the same package as your domain model with the following entry.javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactoryDemoimport java.io.File;import javax.xml.bind.*;public class Demo { public static void main(String[] args) throws Exception { JAXBContext jc = JAXBContext.newInstance(Person.class); Unmarshaller unmarshaller = jc.createUnmarshaller(); File xml = new File("src/forum14455596/input.xml"); Person person = (Person) unmarshaller.unmarshal(xml); System.out.println("Person: " + person.locator.getLineNumber()); System.out.println("Address: " + person.address.locator.getLineNumber()); }}OutputJane Doe 31 A Street 5Person: 2Address: 4 这篇关于使用jaxb进行解组时各个XML元素的行号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-23 19:50