Page 1 of 1

Modify custom xml of document

PostPosted: Tue May 22, 2012 2:13 pm
by anil_ah
Hi jason,
I am loading the custom xml by using the following code snippet

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
     String itemId = "{2e80dc51-dc3b-4296-bf09-3e682aab035a}".toLowerCase();
                         CustomXmlDataStoragePart customXmlDataStoragePart = wordMLPackage.getCustomXmlDataStorageParts().get(itemId);
                         CustomXmlDataStorage customXmlDataStorage = customXmlDataStoragePart.getData();

  Document doc= ((CustomXmlDataStorageImpl)customXmlDataStorage).getDocument();
  // remove  source of fund first row
                 doc.removeChild(((CustomXmlDataStorageImpl)customXmlDataStorage).xpathGetNodes("/investmentTemplate/sourceoffunds", " ").get(0));
 
Parsed in 0.015 seconds, using GeSHi 1.0.8.4


Following one is custom xml structure

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
<investmentTemplate>
<sourceoffunds>
                <fund>
                        <serviceName></serviceName>
                        <productname>pname</productname>
                        <owner>owner</owner>
                        <value>value</value>
                </fund>
                <exist></exist>
        </sourceoffunds>
</investmentTemplate>
 
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


i want to remove the fund elment first as its mapped to ducument with static data and then want to keep on ading more fund elment here,as it will be a repeat of table row,but its give following error

Code: Select all
org.w3c.dom.DOMException: NOT_FOUND_ERR: An attempt is made to reference a node in a context where it does not exist.
   at org.apache.xerces.dom.ParentNode.internalRemoveChild(Unknown Source)
   at org.apache.xerces.dom.ParentNode.removeChild(Unknown Source)
   at org.apache.xerces.dom.CoreDocumentImpl.removeChild(Unknown Source)
   at com.citigroup.citiplanner.factfinder.ljo.InvestmentMFSPDCASoaReport.getwordMLPackage(InvestmentMFSPDCASoaReport.java:125).


please help how to modify this xml here

Re: Modify custom xml of document

PostPosted: Tue May 22, 2012 4:18 pm
by jason
anil_ah wrote:
Code: Select all
org.w3c.dom.DOMException: NOT_FOUND_ERR: An attempt is made to reference a node in a context where it does not exist.
   at org.apache.xerces.dom.ParentNode.internalRemoveChild(Unknown Source)
   at org.apache.xerces.dom.ParentNode.removeChild(Unknown Source)
   at org.apache.xerces.dom.CoreDocumentImpl.removeChild(Unknown Source)
   at com.citigroup.citiplanner.factfinder.ljo.InvestmentMFSPDCASoaReport.getwordMLPackage(InvestmentMFSPDCASoaReport.java:125).




anil_ah wrote:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  Document doc= ((CustomXmlDataStorageImpl)customXmlDataStorage).getDocument();
  // remove  source of fund first row
                 doc.removeChild(((CustomXmlDataStorageImpl)customXmlDataStorage).xpathGetNodes("/investmentTemplate/sourceoffunds", " ").get(0));
 
Parsed in 0.014 seconds, using GeSHi 1.0.8.4




removeChild is used to remove a child from its parent.

Here, sourceoffunds is probably not a child of the document element: print out the node name of doc to check.

It would be simpler not to use xpath to do this operation, but if you must, then having found the node n you want to remove, do something like n.getParent().removeChild(n)

Re: Modify custom xml of document

PostPosted: Tue May 22, 2012 5:09 pm
by anil_ah
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
Document doc= ((CustomXmlDataStorageImpl)customXmlDataStorage).getDocument();
                         System.out.println(doc.getNodeName()+"node name");
Parsed in 0.013 seconds, using GeSHi 1.0.8.4



node name i am getting here is
#document

please share best partice example to modify the custom xml that would be highly appreciated

Re: Modify custom xml of document

PostPosted: Tue May 22, 2012 5:32 pm
by anil_ah
By Using n.getParent().removeChild(n), i am able to remove the node,Thanks a lot

Please share the best way to Modify the Custom xml

Re: Modify custom xml of document

PostPosted: Tue May 22, 2012 6:39 pm
by jason
You can either modify it, or replace the whole document (as to which see previous posts).

I've already given you hints as to how to modify it. Its really just basic W3C DOM manipulation, for which you'd probably be better asking elsewhere.