Rajendra Prasad Murakonda wrote:
You get an exception while calling nsIRDFContainer.AppendElement, but if
  you have removed all nodes, which node do you append the new node to ?
For adding the first element, I need to use AppendElement() only, right?
My root is there but it's empty, then I am adding the new element.
I presume you kept the root node (the one with a RDF id set in the ref
attribute, "urn:root" for instance).
My root node is this:
<RDF:Seq RDF:about="http://path/to/all-nodes/";>
   </RDF:Seq>
when I do MakeSeq(), the above will get created and all the elements added
using AppendElemtn() are part of this.

Ok, so you never remove the root node.

Just keep in mind that even if the root node is empty, the root resource exists in the RDF datasource, defining at least the properties http://www.w3.org/1999/02/22-rdf-syntax-ns#instanceOf and http://www.w3.org/1999/02/22-rdf-syntax-ns#Seq. Removing the corresponding RDF triplets would cause strange things to happen.

Here is the code I used many times without problem for adding a node in a tree:

function createTreeNode(ds,resValue,parentNode) {
  RDF = Components.classes["@mozilla.org/rdf/rdf-service;1"].
    getService().QueryInterface(Components.interfaces.nsIRDFService);
  RDFCUtils = Components.classes["@mozilla.org/rdf/container-utils;1"].
    getService().QueryInterface(
      Components.interfaces.nsIRDFContainerUtils);

  var node=null;
  if(resValue==null) {
        node=RDF.GetAnonymousResource();
  } else {
        node=RDF.GetResource(resValue);
  }
  ds.Assert(node,
    RDF.GetResource(
      "http://www.w3.org/1999/02/22-rdf-syntax-ns#instanceOf";),
    RDF.GetResource("http://www.w3.org/1999/02/22-rdf-syntax-ns#Seq";),
    true);
  ds.Assert(node,
    RDF.GetResource(
      "http://www.w3.org/1999/02/22-rdf-syntax-ns#nextVal";),
    RDF.GetLiteral("1"),true);

  if(parentNode!=null) {
    if(typeof(parentNode)=="string")
        parentNode=RDF.GetResource(parentNode);
    parentNode=RDFCUtils.MakeSeq(ds,parentNode);
    parentNode.AppendElement(node);
  }
  return node;
}

To create a new element as a child of your root node, you call:
createTreeNode(yourDatasource,null,"http://path/to/all-nodes/";)

You should give it a try. If it doesn't work, you can dump at different stages, all RDF triplets included in the datasource to see what's going on. I can give you some code for that.


When you removed your nodes, presumably with
nsIRDFContainer.RemoveElement, did you set the second argument
(renumber) to true ?
Yes, I set it to true. But does that matter in this case? The problem comes
after deleting all the nodes.
It could have been that after removing the elements, the datasource is in a bad state with mis-numbered Seqs, and you get the effects when you add a new element.

/mig
_______________________________________________
dev-tech-xpcom mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-xpcom

Reply via email to