Hi Svante,
that is an amazing work. Thank you all guys for your effort. Going to
implement the new knowledge. :)
Have a nice weekend,
David
On 5/11/2018 12:26 AM, Svante Schubert wrote:
Hello David,
Curiosity let me solve the riddle.
I have not used the extra node method by Ralf, but instead, I looked into
the API of the DocumentBuilderFactory. Precisely, googled for
"DocumentBuilder" and "Namespace".
The trick is to make the DocumentFactory namespace-aware and in addition,
add a namespace to your text snippet.
In detail this is being changed:
OLD:
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();
NEW:
String fragment = "<text:p
xmlns:text=\"urn:oasis:names:tc:opendocument:xmlns:text:1.0\"
text:style-name=\"P13\"><text:span
text:style-name=\"T1\">Test</text:span></text:p>";
DocumentBuilderFactory dbf =
DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
And then the test produces an ODF document with the desired output.
Cheers,
Svante
PS: Ralf we should continue to discuss on the dev list as Dave suggested, I
might be offline until Monday, so don't worry, if my response is late. ;-)
ᐧ
2018-05-10 22:34 GMT+02:00 Svante Schubert <[email protected]>:
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
ᐧ
--
S pozravem,
David Michal