Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
110 views
in Technique[技术] by (71.8m points)

java - I can't write correctly words to xml using StAX

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("
");
    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("	");
}

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



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("[ 	]");

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

            return nexSplit;

        }

    }
    return splitText;

}}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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("[ 	]");

    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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...