Page 1 of 1

How to add a node to CustomXMLDataStorage

PostPosted: Wed Jan 25, 2012 6:15 am
by wojtek
Please help.
Suppose my item1.xml file looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<parameters>
<paramNodes>
<p1></p1>
<p2></p2>
</paramNodes>
</parameters>

How to add a node to make it look like this:

<?xml version="1.0" encoding="utf-8" ?>
<parameters>
<paramNodes>
<p1></p1>
<p2></p2>
<p3></p3>
</paramNodes>
</parameters>

I have found the setNodeValueAtXPath method to write values into nodes but I fail to find anything like addNodeAtXPath :?

Re: How to add a node to CustomXMLDataStorage

PostPosted: Wed Jan 25, 2012 7:33 pm
by jason
See data-binding-java-f16/replace-item1-xml-t381.html :-)

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
public org.w3c.dom.Document getDocument() throws Docx4JException
 
Parsed in 0.014 seconds, using GeSHi 1.0.8.4


gives you a w3c DOM document which you can modify as you see fit, using the DOM.

Re: How to add a node to CustomXMLDataStorage

PostPosted: Fri Jan 27, 2012 3:16 am
by wojtek
I was just like someone who has sees a pressure-cooker for the first time and is looking for a spoon to stir in it.
I was trying to use XML document the way I use an SQL table. So I prepared my item1.xml as:
Code: Select all
   <myTable>
      <fieldName1/>
      <fieldName2/>
   </myTable>
and was looking in docx4j for a method to add a field to obtain:
Code: Select all
   <myTable>
      <fieldName1/>
      <fieldName2/>
      <fieldName3/>
   </myTable>

Now I gather it has to be done this way:
Code: Select all
   <table tblName=”myTable”>
      <field fldName =”Name1”/>
      <field fldName=” Name2”/>
   </table>
and to use docx4j methods:
Code: Select all
    Node newField = lastField.cloneNode(false)
    tableNode.appendChild(newField)
then change the attribute fldName of newField to “Name3” to get what I wanted.
Am I right?

Re: How to add a node to CustomXMLDataStorage

PostPosted: Fri Jan 27, 2012 3:59 am
by wojtek
Still however something bothers me.
I found a docx4j method to set node value but I fail to find one to set value of an attribute.
It seems to me in xml in most cases an element can be used instead of an attribute and vice versa.
Why it is not so in docx4j?

Re: How to add a node to CustomXMLDataStorage

PostPosted: Fri Jan 27, 2012 5:05 am
by wojtek
Or maybe it just is so: :idea:
Code: Select all
tableNode.getAttributes().item(0).setNodeValue("My table name");

Re: How to add a node to CustomXMLDataStorage

PostPosted: Wed Feb 01, 2012 6:59 pm
by jason
Your XML can contain whatever element names and attributes you want (within the constraints imposed by the XML Spec). docx4j does not impose any additional limitations. The manipulations you are performing are not even using the docx4j API, but rather, the DOM. Google will tell you how to use that API, as well as offering you alternatives :-)