I am working with a small project, graphipedia (It is used to import dumps from wikipedia or some other wiki), and through this I am importing a dump of wikiquotes. This project uses stax internally to parse the xml (that is, the dump)
At some point in the XML reading process, the code reads characters (between and ), and the code appends to a variable variable StringBuilder, but for some reason the method does not add not a single character to the textBuffer variable (of type StringBuilder)
Here's the code:
package org.graphipedia.dataimport;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.events.XMLEvent;
import org.codehaus.stax2.XMLInputFactory2;
public abstract class SimpleStaxParser {
private static final String STDIN_FILENAME = "-";
private static final XMLInputFactory XML_INPUT_FACTORY = XMLInputFactory2.newInstance();
private final List<String> interestingElements;
public SimpleStaxParser(List<String> interestingElements) {
this.interestingElements = interestingElements;
}
protected abstract void handleElement(String element, String value);
public void parse(String fileName) throws IOException, XMLStreamException {
if (STDIN_FILENAME.equals(fileName)) {
parse(System.in);
} else {
parse(new FileInputStream(fileName));
}
}
private void parse(InputStream inputStream) throws IOException, XMLStreamException {
XMLStreamReader reader = XML_INPUT_FACTORY.createXMLStreamReader(inputStream, "UTF-8");
try {
parseElements(reader);
} finally {
reader.close();
inputStream.close();
}
}
private void parseElements(XMLStreamReader reader) throws XMLStreamException {
LinkedList<String> elementStack = new LinkedList<String>();
StringBuilder textBuffer = new StringBuilder();
while (reader.hasNext()) {
switch (reader.next()) {
case XMLEvent.START_ELEMENT:
elementStack.push(reader.getName().getLocalPart());
textBuffer.setLength(0);
break;
case XMLEvent.END_ELEMENT:
String element = elementStack.pop();
if (isInteresting(element)) {
handleElement(element, textBuffer.toString().trim());
}
break;
case XMLEvent.CHARACTERS:
if (isInteresting(elementStack.peek())) {
textBuffer.append(reader.getText());
}
break;
}
}
}
private boolean isInteresting(String element) {
return interestingElements.contains(element);
}
}
The line of the code that is bringing me problems is this
textBuffer.append(reader.getText());
On that line, reader.getText()
returns the following:
lo que pasó, pasó por una razón...
'''Neo''': ¿Y qué razón es esa?
'''Smith''': Yo lo maté, señor Anderson, lo vi morir... Con cierta satisfacción, debo decir. Y luego algo pasó. Algo que sabía que era imposible, pero aún así pasó: usted me destruyó, señor Anderson... Después, cuando supe las reglas, entendí lo que debí haber hecho, pero no lo hice. No podía, fui obligado a quedarme, fui obligado a desobedecer... Y ahora aquí estoy por su culpa, señor Anderson. Por su culpa, ya no soy un agente de este sistema. Por su culpa cambié, me desconecté. Un hombre libre por decir algo, como usted, aparentemente libre.
'''Neo''': ¡Felicidades!
'''Smith''': Gracias... Pero, como sabrá, las apariencias engañan, lo cual me regresa a la razón por la que estoy aquí. No estamos aquí por ser libres. Estamos aquí por no ser libres. No hay razón de escapatoria, ni propósitos de negación. Porque, como sabemos, sin propósitos, no existiríamos...
'''Clones''': Propósito fue lo que nos creó... propósito lo que nos conecta, propósito lo que nos impulsa, lo que nos guía, lo que nos controla, es el propósito lo que define, propósito lo que nos une.
'''Smith''': Estamos aquí por culpa suya, señor Anderson. Estamos aquí para quitarle lo que trató de quitarnos a nosotros ¡Propósito!
[[Categoría:Películas]]
[[en:The Matrix (franchise)]]
[[sl:Matrica]]
Before the append method is executed, the variable textBuffer has a value in "count" of 30643, a "capacity" of 64254, and the text to add has a length of 1352.
The information in which the parser is working is at: link (It's too big to show directly here)
Steps to reproduce this problem:
Get the dump , download graphipedia, unzip it and build it with maven ( mvn package
). Run ExtractLinks
from Eclipse
or a similar IDE, in order to be able to debug the code correctly.