Page 1 of 1

RVXML problem

Posted: Mon Aug 29, 2005 12:20 pm
by martindholmes
Hi there,

I'm trying to use a TRichViewXML component to save and load data to and from a TRichView with an associated TRVStyle. Basically, I'm creating a TXMLTag, and trying to save data to that tag. But when I try to save the data, I get an access violation. The TXMLTag is created as part of an object called a TSnippet, and it's created without a parent XMLTag, and without a root TXMLTree:

XMLTag := TXMLTag.Create('snippet-text', nil, nil);

Then I set the Unicode property to match the Unicode property of the TRichView:

XMLTag.Unicode := True;

I don't need anything except the TXMLTag, because each TSnippet object is going to write itself out as part of a larger structure eventually. However, when I try to save data from the TRichView to the TXMLTag, I get an AV here:

procedure TRichViewXML.SaveToXML(Node: TXMLTag);
var
AOnSaveControl: TRVXMLSaveControlEvent;
AOnUnknownItem: TRVXMLUnknownItemEvent;
begin
if not Assigned(FRichView) then Exit;

if Node.Root.UnicodeTags.IndexOf('utext') = -1 then //AV HAPPENS HERE

Can anyone tell me what the problem might be here?

All help appreciated,
Martin

Posted: Mon Aug 29, 2005 5:49 pm
by Sergey Tkachenko
I am not sure that TXMLTag can be used outside of TRVXML.

Some time ago I made an example how to save 2 richviews in one XML, may be it can be useful

Code: Select all

uses RichViewXML, RVXMLBase;

RichView1, RichView2 - trichviews to load/save.
RichViewXML1 - trichviewxml.
This example saves and loads two documents in 'd:\1.xml'

// Saving
procedure TForm1.Button1Click(Sender: TObject);
var
  T: TXMLTree;
  MainRoot, DocRoot: TXMLTag;
begin
  T := TXMLTree.Create(RichViewXML1.Encoding);
  try
    T.BaseFilePath := RichViewXML1.BaseFilePath;
    T.Items.AddTag(RichViewXML1.RootElement, T, T);
    MainRoot := T.Items[0];

    MainRoot.Items.AddTag('document1', T.Items[0], T);
    DocRoot := MainRoot.Items[MainRoot.Items.Count-1];
    RichViewXML1.RichView := RichView1;
    RichViewXML1.SaveToXML(DocRoot);

    MainRoot.Items.AddTag('document2', T.Items[0], T);
    DocRoot := MainRoot.Items[MainRoot.Items.Count-1];
    RichViewXML1.RichView := RichView2;
    RichViewXML1.SaveToXML(DocRoot);

    T.SaveToFile('d:\1.xml', RichViewXML1.WantTabs);
  finally
    T.Free;
  end;
end;

// Loading
procedure TForm1.Button2Click(Sender: TObject);
var
  T: TXMLTree;
  MainRoot, DocRoot: TXMLTag;
begin
  T := TXMLTree.Create(RichViewXML1.Encoding);
  try
      T.BaseFilePath := RichViewXML1.BaseFilePath;
      T.UnicodeTags.Add('utext');
      T.LoadFromFile('d:\1.xml');

      RichViewXML1.RichView := RichView1;
      MainRoot := T.Items.FindTagOfName(RichViewXML1.RootElement);
      if MainRoot<>nil then begin
        DocRoot := MainRoot.Items.FindTagOfName('document1');
        if DocRoot<>nil then
          RichViewXML1.LoadFromXML(DocRoot);

        RichViewXML1.RichView := RichView2;
        DocRoot := MainRoot.Items.FindTagOfName('document2');
        if DocRoot<>nil then
          RichViewXML1.LoadFromXML(DocRoot);
      end;
  finally
    T.Free;
  end;
  RichView1.Format;
  RichView2.Format;
end;

Posted: Mon Aug 29, 2005 8:19 pm
by Guest
Thanks Sergey -- that helps a lot. What I need to do is exactly that -- store multiple documents in one XML file -- but there will be lots of them, and they'll all be pretty small. Each doc is encapsulated as a TSnippet object, and I was hoping to have each one store its data in a TXMLTag until file-save time, then spit out all the TXMLTags into a file. But I guess I can approach it differently.

I'd still love to know why a TXMLTag can't just save and load data, though; it would be so convenient. I'll keep reading through the source.

Cheers,
Martin

Posted: Tue Aug 30, 2005 12:26 pm
by Guest
Reading your code, I see that you're able to do this:

RichViewXML1.SaveToXML(DocRoot);

where DocRoot is a TXMLTag, which isn't attached to any larger structure. That's basically all I need to do myself. But you're not instantiating your TXMLTag, you're just declaring it and passing it to the save function. Maybe that's what I need to do... I'll try it.

Cheers,
Martin

Posted: Tue Aug 30, 2005 12:38 pm
by Sergey Tkachenko
DocRoot is taken from the XML tree:

MainRoot.Items.AddTag('document1', T.Items[0], T);
DocRoot := MainRoot.Items[MainRoot.Items.Count-1];