Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2006-06

Modify XML Document

4 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Felipe Monteiro de Carvalho

Hello,

I am trying to use the units XMLRead, XMLWrite, DOM to modify the structure of a XML file.

Reading the XML file is very easy and works very well, but I am lost about how to modify it´s structure. Specifically I want to add a new "brother" to a node. I try to create a DomNode and then add it, but I cannot change the NodeName property. So how can I change the name of a newly created node??

Also, how can I add a new Attribute to a DomNode??

The following code will give an error because NodeName is read-only:

var
  NovoNo: TDomNode;
begin
    Posicao := TreeView1.Selected.Index;
    NovoNo := TDomNode.Create(XMLDoc);
    NovoNo.NodeName := 'categoria';
    XMLDoc.DocumentElement.InsertBefore(NovoNo,
     XMLDoc.DocumentElement.ChildNodes.Item[Posicao]);

I looked at the examples on fpcsrc, but there are only very basic ones about xml.

Any help e very welcome.

thanks a lot,

Felipe Monteiro de Carvalho

Reply by email

Tony Pelton
to Felipe Monteiro de Carvalho

On 6/22/06, Felipe Monteiro de Carvalho
wrote:

-- snip TDOCDocument interface source from FPC 2.0.2 --
TDOMDocument = class(TDOMNode_WithChildren)
  protected
    FDocType: TDOMDocumentType;
    FImplementation: TDOMImplementation;
    function GetDocumentElement: TDOMElement;
  public
    property DocType: TDOMDocumentType read FDocType;
    property Impl: TDOMImplementation read FImplementation;
    property DocumentElement: TDOMElement read GetDocumentElement;
    function CreateElement(const tagName: DOMString): TDOMElement; virtual;
    function CreateDocumentFragment: TDOMDocumentFragment;
    function CreateTextNode(const data: DOMString): TDOMText;
    function CreateComment(const data: DOMString): TDOMComment;
    function CreateCDATASection(const data: DOMString): TDOMCDATASection;
      virtual;
    function CreateProcessingInstruction(const target, data: DOMString):
      TDOMProcessingInstruction; virtual;
    function CreateAttribute(const name: DOMString): TDOMAttr; virtual;
    function CreateEntityReference(const name: DOMString): TDOMEntityReference;
      virtual;
    // Free NodeList with TDOMNodeList.Release!
    function GetElementsByTagName(const tagname: DOMString): TDOMNodeList;
    // Extensions to DOM interface:
    constructor Create;
    function CreateEntity(const data: DOMString): TDOMEntity;
  end;
-- snip --

it's been a bit since i played with the FPC implementation of the XML stuff, but i've done quite a bit of work with DOM in Java.

i've included the interface from dom.pp in the 2.0.2 source above for reference.

first thing, you need to understand is that TDOMDocument is the "handle" to the DOM.

so you need to start with an instance of one of these, either by creating one, or by "loading" an XML document.

from there, you ...

a) create "nodes" by calling methods on the TDOMDocument (the parent of all nodes).

i included some of the specific and more common methods from TDOMDocument below, such as the <CreateElement> function, and the <CreateAttribute> function for instance.

-- snip --
    function CreateElement(const tagName: DOMString): TDOMElement; virtual;
    function CreateTextNode(const data: DOMString): TDOMText;
    function CreateCDATASection(const data: DOMString): TDOMCDATASection;
      virtual;
    function CreateAttribute(const name: DOMString): TDOMAttr; virtual;
-- snip --

again, you call these on a properly created instance of the TDOMDocument ... because nodes must be "owned" by a document.

b) once you have a node, and you've mutated it (set its value etc ...) to your liking, you can then parent it into the XMLDocument by walking the DOM ( getChildren() etc ...) until you get to a node that you want to attach your newly created node to, as a child or sibling or what have you.

you create attributes in the same way, by calling methods on the TDOMDocument, which return instances of the attribute class, and then they can be attached to a node in the tree as required, becoming an attribute of that node.

you never instantiate nodes and attributes directly, at least not ime.

Felipe Monteiro de Carvalho

Tony

X-SA user ? 0.5.1 is out ! XData 0.1 for X-SA is out ! http://x-plane.dsrts.com

Reply by email

Unknown author
to Felipe Monteiro de Carvalho: I am trying to use the units XMLRead, XMLWrite, DOM to modify the structure of a XML file.

Felipe,

Have a look at the lazdocfrm.pas file located in lazarus/ide. for a working example.

Darius

Show quoted text 37 lines
Hello, I am trying to use the units XMLRead, XMLWrite, DOM to modify the structure of a XML file. Reading the XML file is very easy and works very well, but I am lost about how to modify it´s structure. Specifically I want to add a new "brother" to a node. I try to create a DomNode and then add it, but I cannot change the NodeName property. So how can I change the name of a newly created node?? Also, how can I add a new Attribute to a DomNode?? The following code will give an error because NodeName is read-only: var NovoNo: TDomNode; begin Posicao := TreeView1.Selected.Index; NovoNo := TDomNode.Create(XMLDoc); NovoNo.NodeName := 'categoria'; XMLDoc.DocumentElement.InsertBefore(NovoNo, XMLDoc.DocumentElement.ChildNodes.Item[Posicao]); I looked at the examples on fpcsrc, but there are only very basic ones about xml. Any help e very welcome. thanks a lot, -- Felipe Monteiro de Carvalho _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email