当前位置: 首页 > 知识库问答 >
问题:

使用JAXB获取可以是任何类型的内容和标记XML的属性

叶浩荡
2023-03-14
<attribute>
    <id>9137755520013147917</id>
    <name>Routeur</name>
    <value xsi:type="EntityType" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <id>9139898058113224747</id>
        <name>pe-cdg1200e-2sn311-01</name>
        <objectTypeId>9138560130813318972</objectTypeId>
        <objectTypeName>Routeur PE</objectTypeName>
        <classId>3061830882013808530</classId>
        <className>Elément de Réseau</className>
    </value>
</attribute>
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "id",
    "name",
    "ismultiple",
    "type",
    "value"
})
public static class Attribute {

    @XmlElement(required = true)
    protected BigInteger id;
    protected String name;
    protected String ismultiple;
    protected String type;
    @XmlElement(required = true)
    protected Value value;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "")
public class Value {

public Value() {
}

@XmlAttribute(namespace="http://www.w3.org/2001/XMLSchema-instance")
private String type;

@XmlValue
private Object value ;

//getter setter
Exception in thread "main" java.lang.NullPointerException
at com.sun.xml.internal.bind.v2.runtime.reflect.TransducedAccessor.get(TransducedAccessor.java:154)
at com.sun.xml.internal.bind.v2.runtime.property.ValueProperty.<init>(ValueProperty.java:66)
at com.sun.xml.internal.bind.v2.runtime.property.PropertyFactory.create(PropertyFactory.java:95)
at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.<init>(ClassBeanInfoImpl.java:145)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getOrCreate(JAXBContextImpl.java:479)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getOrCreate(JAXBContextImpl.java:498)
at com.sun.xml.internal.bind.v2.runtime.property.SingleElementNodeProperty.<init>(SingleElementNodeProperty.java:90)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.sun.xml.internal.bind.v2.runtime.property.PropertyFactory.create(PropertyFactory.java:113)
at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.<init>(ClassBeanInfoImpl.java:145)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getOrCreate(JAXBContextImpl.java:479)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getOrCreate(JAXBContextImpl.java:498)
at com.sun.xml.internal.bind.v2.runtime.property.ArrayElementProperty.<init>(ArrayElementProperty.java:97)
at com.sun.xml.internal.bind.v2.runtime.property.ArrayElementNodeProperty.<init>(ArrayElementNodeProperty.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.sun.xml.internal.bind.v2.runtime.property.PropertyFactory.create(PropertyFactory.java:113)
at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.<init>(ClassBeanInfoImpl.java:145)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getOrCreate(JAXBContextImpl.java:479)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getOrCreate(JAXBContextImpl.java:498)
at com.sun.xml.internal.bind.v2.runtime.property.SingleElementNodeProperty.<init>(SingleElementNodeProperty.java:90)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.sun.xml.internal.bind.v2.runtime.property.PropertyFactory.create(PropertyFactory.java:113)
at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.<init>(ClassBeanInfoImpl.java:145)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getOrCreate(JAXBContextImpl.java:479)
at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.<init>(ClassBeanInfoImpl.java:132)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getOrCreate(JAXBContextImpl.java:479)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(JAXBContextImpl.java:305)
at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(JAXBContextImpl.java:1100)
at com.sun.xml.internal.bind.v2.ContextFactory.createContext(ContextFactory.java:143)
at com.sun.xml.internal.bind.v2.ContextFactory.createContext(ContextFactory.java:110)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:202)
at javax.xml.bind.ContextFinder.find(ContextFinder.java:376)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:574)
at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:522)
at test.JAXB.main(JAXB.java:25)

公共类JAXB{

/**
 * @param args
 * @throws JAXBException
 */
public static void main(String[] args) throws JAXBException {
    File xmlFile = new File("C:\\activation.xml");

    //JAXBContext jaxbContext = JAXBContext.newInstance(ObjectFactory.class);
    JAXBContext jaxbContext = JAXBContext.newInstance(ServiceOrderType.class);

    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
    ServiceOrderType scf = (ServiceOrderType) jaxbUnmarshaller.unmarshal(xmlFile);
    System.out.println(scf);

    for (Attribute attribute : scf.getAttributes().getAttribute()) {

        if (attribute.getName().equals("RESEAU SERVEUR XML")) {

            /*if (attribute.getValue() instanceof EntityListType){
             EntityListType reseau = (EntityListType) attribute.getValue();
             System.out.println( "name: " + reseau.getEntity().get(0).getName());
             //Element content  = (Element) attribute.getValue();
             //System.out.println(content.getAttributes().toString());

             }*/
            System.out.println("value type: " + attribute.getValue().getType());

            //List<EntityType> reseau = (List<EntityType>) attribute.getValue;
            EntityListType reseau = (EntityListType) attribute.getValue().getObject();
            System.out.println("*******>" + reseau.getEntity().get(0).getName());
        // Object ob = attribute.getValue().getObject();

            System.out.println(attribute.getName() + "---------------->" + attribute.getValue().getObject());
        }
    }
    /*
     for(Attribute attribute :  scf.getComponents().getEntity().get(0).getAttributes().getAttribute()){

     if(attribute.getName().equals("RESEAU SERVEUR XML")){

     System.out.println(attribute.getName() + "---------------->" + attribute.getValue());
     //System.out.println(attribute.getValue().get);
     }
     }*/
}

共有1个答案

陈项禹
2023-03-14

首先,必须将指定为根源。由于它包含许多子节点,因此必须将其内容指定为对象数组。不能使用@xmlvalue,必须使用@xmlanyelement(lax=true)

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "")
class Value {

    @XmlAttribute(namespace="http://www.w3.org/2001/XMLSchema-instance")
    private String type;

    @XmlAnyElement(lax=true) 
    private Object[] body;

    public String getType() {
        return type;
    }


    public Object[] getBody() {
        return body;
    }
}

然后,为了有一个字符串表示,您可以这样做

Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

StringWriter sw = new StringWriter();
jaxbMarshaller.marshal(example.getAttributes().get(0).getValue(), sw);
System.out.println(sw.toString());

当然,如果您有多个value元素,您必须迭代,但这适用于您给出的示例。

 类似资料:
  • 这是我的xml: 我为这个xml使用了JAXB和unMarshall,我可以得到描述和外部密钥。但是我不能得到有价值的属性名称。 > 这是我的java类: 组织JAVA 银行JAVA 如何获取属性名称和值?谢谢你

  • 我想使用jsoup从网页中提取内容。这些值在内部标签中,如何提取这些值? 例如 我想提取锚点标签中的内容首页*将如何做到这一点?

  • 本文向大家介绍使用jQuery获得内容以及内容的属性,包括了使用jQuery获得内容以及内容的属性的使用技巧和注意事项,需要的朋友参考一下 Tips: 1、点击事件节点的选择 #botton 2、alert 的代码规则  ("Value: " + $("#id").html) 今天的分享就先到这里了,后续我们还将持续更新一些新的jQuery实例。

  • 我不确定我的方向是否正确,但我试图实现的目标相当复杂,至少对我来说是这样。所以我要找的是找到HTML类标签中的内容,并在我想要的任何地方显示这些内容。让我们以一个网站上的在线产品为例,假设标题上的代码写得如下: 是的,我知道我可以使用如下变量: HTML: 风格:

  • 我有一些Java类扩展了一个公共超类。我想用JAXB对它们进行封送/解封,而不会在XML表示中看到超类标记。包含超类的模块不知道当前上下文中可用的子类。 以下是我试图做的事情和我的方法的最小例子: 示例XML: 这应该是一个等效的 Java 表示形式,其中具有类型的成员,该成员是实例的父: 我主要关心的是从XML表示到Java对象。请注意,我明确不想在XML文件中编写超类标记,因此类似于

  • 我陷入了一个棘手的问题,我应该使用JAXB库取消marshaX输入流,除了XML结构没有帮助。 我的问题是:标记用于值为>的的