使用JAXB部分解组XML以跳过某些xmlElement

时间:2013-05-20 19:22:46

标签: xml-parsing xml-serialization jaxb2 unmarshalling

我想使用JAXB将XML文件解组为java对象。 XML文件非常大并且包含一些节点,在某些情况下我想跳过这些节点以提高性能,因为客户端java程序无法编辑这些元素。

示例XML如下:

<Example id="10" date="1970-01-01" version="1.0"> 
    <Properties>...</Properties>
    <Summary>...</Summary>
    <RawData>
        <Document id="1">...</Document>
        <Document id="2">...</Document>
        <Document id="3">...</Document>
        ------
        ------
    </RawData>
    <Location></Location>
    <Title></Title>
    ----- // more elements
</Example>

我有两个用例:

  • 将包含Properties,Summaries,RawData等的示例对象解组,而不跳过任何RawData。 (已完成此部分)
  • 解组为不包含RawData的示例对象。嵌套在RawData中的元素非常大,因此不希望在此用例中阅读此内容。

现在我想解组XML,以便可以跳过RawData。我尝试了this link提供的技术。

使用上面链接中提供的技术也会跳过RawData之后的所有元素。

2 个答案:

答案 0 :(得分:3)

我已使用以下代码修复了XMLEventReader的问题:

public class PartialXmlEventReader implements XMLEventReader {

private final XMLEventReader reader;
private final QName qName;
private boolean skip = false;

public PartialXmlEventReader(final XMLEventReader reader, final QName element) {
    this.reader = reader;
    this.qName = element;
}

@Override
public String getElementText() throws XMLStreamException {
    return reader.getElementText();
}

@Override
public Object getProperty(final String name) throws IllegalArgumentException {
    return reader.getProperty(name);
}

@Override
public boolean hasNext() {
    return reader.hasNext();
}

@Override
public XMLEvent nextEvent() throws XMLStreamException {
    while (isEof(reader.peek())) {
        reader.nextEvent();
    }

    return reader.nextEvent();
}

@Override
public XMLEvent nextTag() throws XMLStreamException {
    return reader.nextTag();
}

@Override
public XMLEvent peek() throws XMLStreamException {
    return reader.peek();
}

@Override
public Object next() {
    return reader.next();
}

@Override
public void remove() {
    reader.remove();
}

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

private boolean isEof(final XMLEvent e) {
    boolean returnValue = skip;
    switch (e.getEventType()) {
    case XMLStreamConstants.START_ELEMENT:
        final StartElement se = (StartElement) e;
        if (se.getName().equals(qName)) {
            skip = true;
            returnValue = true;
        }
        break;
    case XMLStreamConstants.END_ELEMENT:
        final EndElement ee = (EndElement) e;
        if (ee.getName().equals(qName)) {
            skip = false;
        }
        break;
    }
    return returnValue;
}

}

Unmarshalling只是将这个eventReader传递给unmarshal方法

final JAXBContext context = JAXBContext.newInstance(classes);
    final Unmarshaller um = context.createUnmarshaller();
    Reader reader = null;
    try {
        reader = new BufferedReader(new FileReader(xmlFile));
        final QName qName = new QName("RawData");
        final XMLInputFactory xif = XMLInputFactory.newInstance();
        final XMLEventReader xmlEventReader = xif.createXMLEventReader(reader);
        final Example example =
                (Example) um.unmarshal(new PartialXmlEventReader(xmlEventReader, qName));
        }
    } finally {
        IOUtils.closeQuietly(reader);
    }

答案 1 :(得分:1)

我希望这会有所帮助

try {
        // First create a new XMLInputFactory
        XMLInputFactory inputFactory = XMLInputFactory.newInstance();
        // Setup a new eventReader
        InputStream in = new FileInputStream("myXml");
        XMLEventReader eventReader = inputFactory.createXMLEventReader(in);
        // Read the XML document
        Example example = null;

        while (eventReader.hasNext()) {
            XMLEvent event = eventReader.nextEvent();

            if (event.isStartElement()) {
                StartElement startElement = event.asStartElement();
                // If we have a example element we create a new example
                if (startElement.getName().getLocalPart().equals("Example")) {
                    example = new Example();
                    // We read the attributes from this tag and add the date
                    //  and id attribute to our object
                    Iterator<Attribute> attributes = startElement
                            .getAttributes();
                    while (attributes.hasNext()) {
                        Attribute attribute = attributes.next();
                        if (attribute.getName().toString().equals("date")) {
                            example.setDate(attribute.getValue());
                        } else if (attribute.getName().toString().equals("id")) {
                            example.setId(attribute.getValue());
                        }

                    }
                }

                 //get the Properties tag and add to object example
                if (event.isStartElement()) {
                    if (event.asStartElement().getName().getLocalPart()
                            .equals("Properties")) {
                        event = eventReader.nextEvent();
                        example.setProperites(event.asCharacters().getData());
                        continue;
                    }
                }
                 //get the Summary tag and add to object example
                if (event.asStartElement().getName().getLocalPart()
                        .equals("Summary")) {
                    event = eventReader.nextEvent();
                    example.setSummary(event.asCharacters().getData());
                    continue;
                }

                // when you encounter the Rawdata tag just continue
                //without adding it to the object created
                if (event.asStartElement().getName().getLocalPart()
                        .equals("Rawdata")) {
                    event = eventReader.nextEvent();
                    // don't do anything
                    continue;
                }

                //get the location tag and add to object example
                if (event.asStartElement().getName().getLocalPart()
                        .equals("Location")) {
                    event = eventReader.nextEvent();
                    example.setLocation(event.asCharacters().getData());
                    continue;
                }
                // read and add other elements that can be added
            }
            // If we reach the end of an example element/tag i.e closing tag
            if (event.isEndElement()) {
                EndElement endElement = event.asEndElement();
                if (endElement.getName().getLocalPart().equals("Example")) {
                    //do something
                }
            }

        }
    } catch (FileNotFoundException | XMLStreamException e) {
    }