jmurty/java-xmlbuilder

Limit on string length on asString method

Closed this issue · 8 comments

Hi,

When I call the asString() method in the xml object, the output stops at 8188 characters. I have included my relevant code below:

XMLBuilder questionnaire  = null;
        try {
            InputSource is = new InputSource(new InputStreamReader(new FileInputStream(new File("screening_questions.question"))));
            questionnaire = XMLBuilder.parse(is); // Load the basic screening questions to be modified later
        } catch (ParserConfigurationException | FactoryConfigurationError | SAXException | IOException e) {
            e.printStackTrace();
        }

questionnaire.e("Question")
        .e("QuestionIdentifier")
            .t(identifier)
        .up()
        .e("QuestionContent")
            .t("Which pair of words is most similar?")
        .up()
        .e("AnswerSpecification")
            .e("SelectionAnswer")
                .e("MinSelectionCount")
                    .t("1")
                .up()
                .e("MaxSelectionCount")
                    .t("1")
                .up()
                .e("StyleSuggestion")
                    .t("radiobutton")
                .up()
                .e("Selections")
                    .e("Selection")
                        .e("SelectionIdentifier")
                            .t("0")
                        .up()
                        .e("Text")
                            .t(pair.getPairOne())
                        .up()
                    .up()
                    .e("Selection")
                        .e("SelectionIdentifier")
                            .t("1")
                        .up()
                        .e("Text")
                            .t(pair.getPairTwo())
                        .up()
                    .up()
                .up()
            .up()
        .up();

System.out.println(questionnaire.asString());

I wonder if this might be a terminal issue.

Does the same thing happen if you write the XML string to a file in your Java code, instead of to stdout? And are you piping stdout to a file or displaying it in the terminal?

Yes, writing the XML string to a file gives the same problem (that's how I originally wrote it and then I changed to writing to stdout to see if the problem persisted)

I am wondering if there may be a limit on the StringWriter object used in the asString() method?

Ok I tried to get the document separately from the getDocument() method and then writing it to an XML file independently using

TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(new File("C:\\file.xml"));

        // Output to console for testing
        // StreamResult result = new StreamResult(System.out);

        transformer.transform(source, result);

(found at: http://www.mkyong.com/java/how-to-create-xml-file-in-java-dom/)

That correctly outputs the whole XML file.

That's interesting, and odd. I will try to reproduce this to dig into why it is cutting out at that point.

Thanks for the feedback.

As a work-around can you try the toWriter() method? You can write to a File/FileWriter relatively easily using this.

Ah, I've just seen your update. I'm glad doing the transformer dance also works for you.

The toWriter() method should also work and uses less arcane code, but as long as you have a working system for now that's the main thing.

Yes, using the toWriter works! so I suspect it must definitely be something with the StringWriter object?

Ok the problem has magically stopped appearing!

I have added a unit test for this kind of situation -- namely getting an XML String of 0.5 MB to ensure it is complete -- but I cannot reproduce the issue.

It's worrying the issue appeared for you then stopped, but if it is related to the StringWriter there's probably not much I can do about it anyway. I'll close this for now unless you see it recur.