Recent

Author Topic: Change XML element names  (Read 2336 times)

SymbolicFrank

  • Hero Member
  • *****
  • Posts: 792
Change XML element names
« on: November 29, 2021, 12:19:26 pm »
I have a bunch of XML files that have to be converted. (From FastReport 3 to LazReport.) Which includes: changing the names of much of the elements.

Code: Pascal  [Select][+][-]
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <CONFIG>
  3.   <TfrxReport>
  4.     <TfrxReportPage>
  5. ..

should become:

Code: Pascal  [Select][+][-]
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <CONFIG>
  3.   <LazReport>
  4.     <Pages>
  5. ..

But TDOMNode.NodeName isn't writable. I could make a new node with the right name, add all the child nodes to it and replace the old one with the new one. Or perhaps add a SetNodeName method. But there is probably a simpler way?

jamie

  • Hero Member
  • *****
  • Posts: 5129
Re: Change XML element names
« Reply #1 on: November 29, 2021, 12:43:10 pm »
It seems you already know what to do..

I don't think it would be a good idea to rename nodes on the fly, most likely would cause some issues i am sure..


 Build a new DOC on the fly as you read the old one and change the names on the fly. It will be much faster and cleaner.
The only true wisdom is knowing you know nothing

SymbolicFrank

  • Hero Member
  • *****
  • Posts: 792
Re: Change XML element names
« Reply #2 on: November 29, 2021, 03:52:31 pm »
Thanks, I will. I already do that for the top node, because it's a new <CONFIG> one.

jollytall

  • Full Member
  • ***
  • Posts: 157
Re: Change XML element names
« Reply #3 on: November 29, 2021, 04:30:21 pm »
Probably too simple, but what stops you reading it as a text and change the name of the elements with simple string change? Why do you have to "know" that it is an XML doc to change e.g. a <TfrxReport> substring to <LazReport>?

SymbolicFrank

  • Hero Member
  • *****
  • Posts: 792
Re: Change XML element names
« Reply #4 on: November 29, 2021, 07:08:00 pm »
Probably too simple, but what stops you reading it as a text and change the name of the elements with simple string change? Why do you have to "know" that it is an XML doc to change e.g. a <TfrxReport> substring to <LazReport>?
Yes, I have thought about that as well, but when I change the element name, I have to add a new child element with "ClassName=OldName". Doing that in a string seemed too much work.

Btw, the XML format of both FastReport and LazReport is strange. It probably started with Delphi streaming the published properties of the controls to XML, but I don't know how that file looked and I didn't try that with Lazarus so far.

 

TinyPortal © 2005-2018