3

我想使用 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>

我有两个用例:

  • 解组到包含属性、摘要、原始数据等的示例对象,而不跳过任何原始数据。(已经完成了这部分)
  • 解组到排除 RawData 的示例对象。嵌套在 RawData 中的元素非常大,因此不想在此用例中阅读此内容。

现在我想解组 XML 以便可以跳过 RawData。我已经尝试过此链接提供的技术。

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

4

2 回答 2

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);
    }
于 2013-05-21T17:13:03.493 回答
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) {
    }
于 2013-05-20T19:56:44.953 回答