You are viewing a plain text version of this content. The canonical link for it is here.
Posted to odf-users@incubator.apache.org by David Michal <dv...@googlemail.com> on 2018/05/10 10:08:13 UTC

How to add node created from String into document.

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


Re: How to add node created from String into document.

Posted by David Michal <mi...@alis.cz>.
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 <sv...@gmail.com>:
>
>> 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 <dv...@googlemail.com>:
>>
>>> 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


Re: How to add node created from String into document.

Posted by Svante Schubert <sv...@gmail.com>.
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 <sv...@gmail.com>:

> 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 <dv...@googlemail.com>:
>
>> 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
>>
>>
> ᐧ
>

Re: How to add node created from String into document.

Posted by Dave Fisher <da...@comcast.net>.
Hi Svante/Ralf,

Please continue discussions on dev@. Ralf will need to submit PRs to the new Git base. Once you like the way he does it then we can discuss making Ralf a committer so that he can do it himself.

Assuming he is interested.

Regards,
Dave

> On May 10, 2018, at 3:12 PM, Svante Schubert <sv...@gmail.com> wrote:
> 
> You hear it, Ralf!
> No reason to fork and throw at some later time your work over the digital
> fence.
> Instead become the new ruler of the Simple API :)
> Make it your work base!
> 
> Have a nice evening (at least hear it is night),
> Svante
> ᐧ
> 
> 2018-05-10 23:27 GMT+02:00 Dave Fisher <da...@comcast.net>:
> 
>> Hi Svante -
>> 
>> Just a note, but ODFToolkit desperately needs active developers who are
>> enabled to work on the code directly.
>> 
>> Regards,
>> Dave
>> 
>>> On May 10, 2018, at 2:02 PM, Ralf Heydenreich <rh...@justmail.de>
>> wrote:
>>> 
>>> Hi David,
>>> I've solved this problem with creation of a separate node. You can find
>>> an example here:
>>> https://bitbucket.org/fakturamadev/fakturama-2/src/
>> 0b8ffae87c445ddd7e0618ecf643591ee933ba15/Fakturama-Parent/
>> org.fakturama.print.openoffice/src/main/java/org/odftoolkit/simple/common/
>> navigation/PlaceholderNode.java?at=develop&fileviewer=file-view-default
>>> 
>>> 
>>> Look at the "replaceWith(...)"-methods. This is a class from my own
>>> OpenSource project, extending the ODFToolkit with a placeholder
>>> replacement functionality. In short, it replaces a placeholder with the
>>> according String. Unfortunately, I didn't have the time to build a
>>> sample project and contribute it to this project (sorry, Svante ;-) ).
>>> But it's a point at my agenda...
>>> 
>>> Regards,
>>> Ralf.
>>> 
>>> 
>>> Am 10.05.2018 um 22:34 schrieb Svante Schubert:
>>>> 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 <dv...@googlemail.com>:
>>>> 
>>>>> 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
>>>>> 
>>>>> 
>>>> ᐧ
>>>> 
>>> 
>> 
>> 


Re: How to add node created from String into document.

Posted by Svante Schubert <sv...@gmail.com>.
You hear it, Ralf!
No reason to fork and throw at some later time your work over the digital
fence.
Instead become the new ruler of the Simple API :)
Make it your work base!

Have a nice evening (at least hear it is night),
Svante
ᐧ

2018-05-10 23:27 GMT+02:00 Dave Fisher <da...@comcast.net>:

> Hi Svante -
>
> Just a note, but ODFToolkit desperately needs active developers who are
> enabled to work on the code directly.
>
> Regards,
> Dave
>
> > On May 10, 2018, at 2:02 PM, Ralf Heydenreich <rh...@justmail.de>
> wrote:
> >
> > Hi David,
> > I've solved this problem with creation of a separate node. You can find
> > an example here:
> > https://bitbucket.org/fakturamadev/fakturama-2/src/
> 0b8ffae87c445ddd7e0618ecf643591ee933ba15/Fakturama-Parent/
> org.fakturama.print.openoffice/src/main/java/org/odftoolkit/simple/common/
> navigation/PlaceholderNode.java?at=develop&fileviewer=file-view-default
> >
> >
> > Look at the "replaceWith(...)"-methods. This is a class from my own
> > OpenSource project, extending the ODFToolkit with a placeholder
> > replacement functionality. In short, it replaces a placeholder with the
> > according String. Unfortunately, I didn't have the time to build a
> > sample project and contribute it to this project (sorry, Svante ;-) ).
> > But it's a point at my agenda...
> >
> > Regards,
> > Ralf.
> >
> >
> > Am 10.05.2018 um 22:34 schrieb Svante Schubert:
> >> 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 <dv...@googlemail.com>:
> >>
> >>> 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
> >>>
> >>>
> >> ᐧ
> >>
> >
>
>

Re: How to add node created from String into document.

Posted by Dave Fisher <da...@comcast.net>.
Hi Svante -

Just a note, but ODFToolkit desperately needs active developers who are enabled to work on the code directly.

Regards,
Dave

> On May 10, 2018, at 2:02 PM, Ralf Heydenreich <rh...@justmail.de> wrote:
> 
> Hi David,
> I've solved this problem with creation of a separate node. You can find
> an example here:
> https://bitbucket.org/fakturamadev/fakturama-2/src/0b8ffae87c445ddd7e0618ecf643591ee933ba15/Fakturama-Parent/org.fakturama.print.openoffice/src/main/java/org/odftoolkit/simple/common/navigation/PlaceholderNode.java?at=develop&fileviewer=file-view-default
> 
> 
> Look at the "replaceWith(...)"-methods. This is a class from my own
> OpenSource project, extending the ODFToolkit with a placeholder
> replacement functionality. In short, it replaces a placeholder with the
> according String. Unfortunately, I didn't have the time to build a
> sample project and contribute it to this project (sorry, Svante ;-) ).
> But it's a point at my agenda...
> 
> Regards,
> Ralf.
> 
> 
> Am 10.05.2018 um 22:34 schrieb Svante Schubert:
>> 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 <dv...@googlemail.com>:
>> 
>>> 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
>>> 
>>> 
>> ᐧ
>> 
> 


Re: How to add node created from String into document.

Posted by Ralf Heydenreich <rh...@justmail.de>.
Hi David,
I've solved this problem with creation of a separate node. You can find
an example here:
https://bitbucket.org/fakturamadev/fakturama-2/src/0b8ffae87c445ddd7e0618ecf643591ee933ba15/Fakturama-Parent/org.fakturama.print.openoffice/src/main/java/org/odftoolkit/simple/common/navigation/PlaceholderNode.java?at=develop&fileviewer=file-view-default


Look at the "replaceWith(...)"-methods. This is a class from my own
OpenSource project, extending the ODFToolkit with a placeholder
replacement functionality. In short, it replaces a placeholder with the
according String. Unfortunately, I didn't have the time to build a
sample project and contribute it to this project (sorry, Svante ;-) ).
But it's a point at my agenda...

Regards,
Ralf.


Am 10.05.2018 um 22:34 schrieb Svante Schubert:
> 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 <dv...@googlemail.com>:
>
>> 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
>>
>>
> ᐧ
>


Re: How to add node created from String into document.

Posted by Svante Schubert <sv...@gmail.com>.
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 <dv...@googlemail.com>:

> 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
>
>
ᐧ