1

今日、私の JAXB パーサーが突然動かなくなりました。それは数週間働いていました。次のメッセージが表示されます。このコードを数週間変更していません。この設定でいいのかな。


編集2:誰か助けてください!私はこれを理解することはできません。



編集 1: 以下の同じコードを実行する受け入れテストは正常に動作しています。これはクラスローディングの問題だと思います。JDK で JAXB と StAX を使用しています。ただし、jboss 5.1 にデプロイすると、以下のエラーが発生します。1.6.0_26 (ローカル) と 1.6.0_30 (開発サーバー) を使用します。まだ解決策に困惑しています。


予期しない要素 (uri:""、local:"lineEquipmentRecord")。期待される要素は、<{}switchType>、<{}leSwitchId>、<{}nodeAddress>、<{}leId>、<{}telephoneSuffix>、<{}leFormatCode>、<{}groupIdentifier>、<{}telephoneNpa> です。 ,<{}telephoneLine>,<{}telephoneNxx>

これが私のアンマーシャリングクラスです:

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.NoSuchElementException;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.ValidationEvent;
import javax.xml.bind.ValidationEventHandler;
import javax.xml.stream.FactoryConfigurationError;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;


public class PartialUnmarshaller<T> {
    XMLStreamReader reader;
    Class<T> clazz;
    Unmarshaller unmarshaller;

    public PartialUnmarshaller(InputStream stream, Class<T> clazz) throws XMLStreamException, FactoryConfigurationError, JAXBException {
        this.clazz = clazz;
        this.unmarshaller = JAXBContext.newInstance(clazz).createUnmarshaller();
        unmarshaller.setEventHandler(new ValidationEventHandler() {

            @Override
            public boolean handleEvent(ValidationEvent event) {
                System.out.println(event.getMessage());
                return true;
            }
        });
        this.reader = XMLInputFactory.newInstance().createXMLStreamReader(stream);

        /* ignore headers */
        skipElements(XMLStreamConstants.START_DOCUMENT);
        /* ignore root element */
        reader.nextTag();
        /* if there's no tag, ignore root element's end */
        skipElements(XMLStreamConstants.END_ELEMENT);
    }

    public T next() throws XMLStreamException, JAXBException {
        if (!hasNext())
            throw new NoSuchElementException();

        T value = unmarshaller.unmarshal(reader, clazz).getValue();

        skipElements(XMLStreamConstants.CHARACTERS, XMLStreamConstants.END_ELEMENT);
        return value;
    }

    public boolean hasNext() throws XMLStreamException {
        return reader.hasNext();
    }

    public void close() throws XMLStreamException {
        reader.close();
    }

    private void skipElements(Integer... elements) throws XMLStreamException {
        int eventType = reader.getEventType();

        List<Integer> types = new ArrayList<Integer>(Arrays.asList(elements));
        while (types.contains(eventType))
            eventType = reader.next();
    }
}

このクラスは次のように使用されます。

List<MyClass> lenList = new ArrayList<MyClass>();
PartialUnmarshaller<MyClass> pu = new PartialUnmarshaller<MyClass>(
        is, MyClass.class);
while (pu.hasNext()) {
    lenList.add(pu.next());
}

非整列化されている XML:

<?xml version="1.0" encoding="UTF-8"?>
<lineEquipment>
    <lineEquipmentRecord>
        <telephoneNpa>333</telephoneNpa>
        <telephoneNxx>333</telephoneNxx>
        <telephoneLine>4444</telephoneLine>
        <telephoneSuffix>1</telephoneSuffix>
        <nodeAddress>xxxx</nodeAddress>
        <groupIdentifier>LEN</groupIdentifier>
    </lineEquipmentRecord>
    <lineEquipmentRecord>
        <telephoneNpa>111</telephoneNpa>
        <telephoneNxx>111</telephoneNxx>
        <telephoneLine>2222</telephoneLine>
        <telephoneSuffix>0</telephoneSuffix>
        <nodeAddress>xxxx</nodeAddress>
        <groupIdentifier>LEN</groupIdentifier>
    </lineEquipmentRecord>
</lineEquipment>

最後に、MyClass は次のとおりです。

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

/**
 * This class is used as an envelope to hold Martens
 * line equipment information.
 * @author spgezf
 *
 */
@XmlRootElement(name="lineEquipmentRecord")
public class MyClass {

    private String telephoneNpa;
    private String telephoneNxx;
    private String telephoneLine;

    private String telephoneSuffix;
    private String nodeAddress;
    private String groupIdentifier;

    public MyClass(){       
    }


    // Getters and Setters.

    @XmlElement(name="telephoneNpa")
    public String getTelephoneNpa() {
        return telephoneNpa;
    }

    public void setTelephoneNpa(String telephoneNpa) {
        this.telephoneNpa = telephoneNpa;
    }
    @XmlElement(name="telephoneNxx")
    public String getTelephoneNxx() {
        return telephoneNxx;
    }

    public void setTelephoneNxx(String telephoneNxx) {
        this.telephoneNxx = telephoneNxx;
    }
    @XmlElement(name="telephoneLine")
    public String getTelephoneLine() {
        return telephoneLine;
    }

    public void setTelephoneLine(String telephoneLine) {
        this.telephoneLine = telephoneLine;
    }


    @XmlElement(name="telephoneSuffix")
    public String getTelephoneSuffix() {
        return telephoneSuffix;
    }

    public void setTelephoneSuffix(String telephoneSuffix) {
        this.telephoneSuffix = telephoneSuffix;
    }

    @XmlElement(name="nodeAddress")
    public String getNodeAddress() {
        return nodeAddress;
    }

    public void setNodeAddress(String nodeAddress) {
        this.nodeAddress = nodeAddress;
    }

    @XmlElement(name="groupIdentifier")
    public String getGroupIdentifier() {
        return groupIdentifier;
    }

    public void setGroupIdentifier(String groupIdentifier) {
        this.groupIdentifier = groupIdentifier;
    }


}
4

2 に答える 2

1

ありがとう、これは私が克服できなかったクラスローディングの問題なので、JAXB を放棄しました。

于 2013-01-10T22:02:41.327 に答える
0

あなたのPartialUnmarshallerコードは私のために働いた。skipElements以下は、より適切に機能する可能性のある方法を変更する代替バージョンです。

import java.io.InputStream;
import java.util.NoSuchElementException;

import javax.xml.bind.*;
import javax.xml.stream.*;

public class PartialUnmarshaller<T> {
    XMLStreamReader reader;
    Class<T> clazz;
    Unmarshaller unmarshaller;

    public PartialUnmarshaller(InputStream stream, Class<T> clazz) throws XMLStreamException, FactoryConfigurationError, JAXBException {
        this.clazz = clazz;
        this.unmarshaller = JAXBContext.newInstance(clazz).createUnmarshaller();
        unmarshaller.setEventHandler(new ValidationEventHandler() {

            @Override
            public boolean handleEvent(ValidationEvent event) {
                System.out.println(event.getMessage());
                return true;
            }
        });
        this.reader = XMLInputFactory.newInstance().createXMLStreamReader(stream);

        /* ignore headers */
        skipElements();
        /* ignore root element */
        reader.nextTag();
        /* if there's no tag, ignore root element's end */
        skipElements();
    }

    public T next() throws XMLStreamException, JAXBException {
        if (!hasNext())
            throw new NoSuchElementException();

        T value = unmarshaller.unmarshal(reader, clazz).getValue();

        skipElements();
        return value;
    }

    public boolean hasNext() throws XMLStreamException {
        return reader.hasNext();
    }

    public void close() throws XMLStreamException {
        reader.close();
    }

    private void skipElements() throws XMLStreamException {
        while(reader.hasNext() && !reader.isStartElement()) {
            reader.next();
        }
    }

}
于 2012-12-19T11:27:23.067 に答える