I can't write correctly words to xml using StAX

三世轮回 提交于 2019-12-13 08:18:36

问题


I have a problem, I want to get something like this:

<text>
<sentence>
    <word>a</word>
    <word>had</word>
    <word>lamb</word>
    <word>little</word>
    <word>Mary</word>
</sentence>
<sentence>
    <word>Aesop</word>
    <word>and</word>
    <word>called</word>
    <word>came</word>
    <word>for</word>
    <word>Peter</word>
    <word>the</word>
    <word>wolf</word>
</sentence>
<sentence>
    <word>Cinderella</word>
    <word>likes</word>
    <word>shoes</word>
</sentence>

but I get only this:

  <text>
<sentence>
    <word>a</word>
    <word>had</word>
    <word>lamb</word>
    <word>little</word>
    <word>Mary</word>
</sentence></text>

Example text

“ Mary had a little lamb .

Peter called for the wolf , and Aesop came . Cinderella likes shoes..”

And my class

public class StaxWriteXmlTest {

/**
 * @param args
 * @throws FileNotFoundException
 * @throws XMLStreamException
 */
public static void main(String[] args) throws FileNotFoundException,
        XMLStreamException {
    String[] word = initItems();

    String itemXmlFile = "D:\\items.xml";
    // xml event writer with output stream
    XMLOutputFactory xmlOutFactory = XMLOutputFactory.newInstance();
    OutputStream outputStream = new FileOutputStream(itemXmlFile);
    XMLEventWriter eventWriter = xmlOutFactory
            .createXMLEventWriter(outputStream);
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    XMLEventWriter xmlWrite = (XMLOutputFactory.newInstance())
            .createXMLEventWriter(outStream);

    XMLEventFactory eventFactory = XMLEventFactory.newInstance();
    XMLEvent end = createNewLine(eventFactory);
    XMLEvent tab = createTab(eventFactory);

    // Create start tag
    StartDocument startDocument = eventFactory.createStartDocument();
    EndDocument endDocument = eventFactory.createEndDocument();
    eventWriter.add(startDocument);

    // create config open tag
    eventWriter.add(end);
    StartElement configStartElement = eventFactory.createStartElement("",
            "", "text");
    eventWriter.add(configStartElement);
    eventWriter.add(end);





    eventWriter.add(tab);
    StartElement itemStartElement = eventFactory.createStartElement("", "",
            "sentence");
    eventWriter.add(itemStartElement);
    eventWriter.add(end);
    eventWriter.add(tab);

    // add words
    for (String words : word) {
        eventWriter.add(tab);
        createItemNode(eventFactory, eventWriter, "word", words);
        eventWriter.add(tab);
    }
    // eventWriter.add(tab);
    EndElement itemEndElement = eventFactory.createEndElement("", "",
            "sentence");
    eventWriter.add(itemEndElement);
    eventWriter.add(end);

    EndElement configEndElement = eventFactory.createEndElement("", "",
            "text");
    eventWriter.add(configEndElement);
    eventWriter.add(end);

    eventWriter.add(endDocument);
    eventWriter.close();

}

public static void createItemNode(XMLEventFactory eventFactory,
        XMLEventWriter eventWriter, String elementName, String value)
        throws XMLStreamException {
    XMLEvent end = eventFactory.createDTD("\n");
    StartElement startElement = eventFactory.createStartElement("", "",
            elementName);
    eventWriter.add(startElement);
    Characters characters = eventFactory.createCharacters(value);
    eventWriter.add(characters);
    EndElement endElement = eventFactory.createEndElement("", "",
            elementName);
    eventWriter.add(endElement);
    eventWriter.add(end);
}

public static XMLEvent createTab(XMLEventFactory eventFactory) {
    return eventFactory.createDTD("\t");
}

public static XMLEvent createNewLine(XMLEventFactory eventFactory) {
    return eventFactory.createDTD("\n");
}



public static String[] initItems() {

    FileReader fr = null;

    try {
        fr = new FileReader("text.txt");
    } catch (FileNotFoundException e1) {

        e1.printStackTrace();
    }
    BufferedReader inputText = new BufferedReader(fr);
    String text = "", newText = "";
    String allTogether = "";
    try {
        while ((text = inputText.readLine()) != null) {
            newText += text.replaceAll("\\s+", " ").replaceAll(" ,", ",")
                    .replaceAll(" \\.", ".").replaceAll("\\..", ".");

            allTogether = newText.replaceAll("\\s+", " ");

            String[] splitText = allTogether.split(".");

        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    String[] splitText = allTogether.split("[.]");
    for (int i = 0; i < splitText.length; i++) {
        // System.out.println(splitText[i]);
        String[] nexSplit = splitText[i].split("[ \t]");

        for (int x = 0; x < nexSplit.length; x++) {
            Arrays.sort(nexSplit, String.CASE_INSENSITIVE_ORDER);
            // System.out.println(nexSplit[x]);

            return nexSplit;

        }

    }
    return splitText;

}}

回答1:


I haven't parsed through all your code, but this section looks suspicious:

String[] splitText = allTogether.split("[.]");
for (int i = 0; i < splitText.length; i++) {
    // System.out.println(splitText[i]);
    String[] nexSplit = splitText[i].split("[ \t]");

    for (int x = 0; x < nexSplit.length; x++) {
        Arrays.sort(nexSplit, String.CASE_INSENSITIVE_ORDER);
        // System.out.println(nexSplit[x]);

        return nexSplit;

    }

}
return splitText;

Do you really want to return in that inner loop after one iteration? I doubt it.

Also, make sure to close your reader in your initItems() method.



来源:https://stackoverflow.com/questions/28943089/i-cant-write-correctly-words-to-xml-using-stax

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!