Hello David,
to reproduce your problem, I have added a test case in one of the existing
tests of the Simple API.
@Test
public void testXMLasString() {
try {
TextDocument resDoc = (TextDocument)
TextDocument.loadDocument(ResourceUtilities.getTestResourceAsStream("section.odt"));
Section section = resDoc.getSectionByName( "Section1" ); //
this section does exist in the document
// create new node form String
String fragment = "<text:p text:style-name=\"P13\"><text:span
text:style-name=\"T1\">Test</text:span></text:p>";
Node node =
DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new
InputSource(new StringReader(fragment ))).getDocumentElement();
node = section.getOdfElement().getOwnerDocument().importNode(
node, true );
// append new node into section
section.getOdfElement().appendChild( node );
resDoc.save(ResourceUtilities.newTestOutputFile("sectionWithText.odt"));
} catch (Exception e) {
Logger.getLogger(ListItemTest.class.getName()).log(Level.SEVERE, null, e);
Assert.fail(e.getMessage());
}
}
It is not REALLY a test as it always succeeds, but by this, I am able to
debug in the IDE and first of all get a document from your procedure.
You might want to do the same.
An OpenDocument File is a ZIP with XML files within. You can unzip it and
take a look into the content.xml
I usually use the JEdit editor with the "archive" and "XML" plugin to open
the content.xml file without unpacking and make an indent for the XML.
If you do so it reveals the following:
<text:section text:name="Section1" text:style-name="Sect1">
<text:p text:style-name="Standard"/>
<p style-name="P13"><span style-name="T1">Test</span></p></text:section>
The string is inserted, but the prefix, the namespace was not found. You
might get the DOM element and insert the elements one by one with DOM
functions or search for a solution.
Uncertain how it works by String insertion. If you find a way, please come
back and tell :)
Good luck!
Svante
2018-05-10 12:08 GMT+02:00 David Michal <[email protected]>:
> Hi,
>
> I have a code:
>
> // -----------------------------------------------------------------
>
> TextDocument resDoc = TextDocument.loadDocument( someInputStream );
>
> Section section = resDoc.getSectionByName( "Section1" ); // this section
> does exist in the document
>
> // create new node form String
>
> String fragment = "<text:p text:style-name=\"P13\"><text:span
> text:style-name=\"T1\">Test</text:span></text:p>";
>
> Node node = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(
> new InputSource( new StringReader( fragment ) ) ).getDocumentElement();
> node = section.getOdfElement().getOwnerDocument().importNode( node, true
> );
>
> // append new node into section
>
> section.getOdfElement().appendChild( node );
>
> // -----------------------------------------------------------------
>
> The code runs without a problem. But nothing does appear in the section in
> the result document. Please any idea how can I add new nodes created from
> string into the odf document?
>
> Thanks,
>
> David
>
>
ᐧ