Reputation: 11
I'm pretty new at writing scripts. But I'm having a hard time figuring out how to write a simple Macro in VBA to allow multiple xml files to be edited and saved by either removing or replacing a certain text.
For instance I need to delete, from multiple XML files, <!ENTITY % ISOEntities PUBLIC "ISO 8879-1986//ENTITIES...
etc
the other thing is I need to replace "°" to "°deg" within the same XML files.
Thank you for any help.
Upvotes: 1
Views: 478
Reputation: 12019
What's important to note about XML documents is that they're structured data. Trying to alter the XML simply by doing text replacements could lead to malformed XML. Take for example the following XML element:
<test>1 is lower than 2 and 3 is greater than 2</test>
A naive replacement of "is lower than" with "<" and "is greater than" with ">" would yield the following:
<test>1 < 2 and 3 > 2</test>
Now your XML syntax is ruined because the characters <
and >
are used for markup. An XML parser would no longer know if it's intended as simple text or part of an element declaration. So in order to use them as plain text they ought to be represented by entity references:
<test>1 < 2 and 3 > 2</test>
An XML parser which reads the above would do the necessary substitution and if you asked it what the text content of the <test>
element is, it would respond with 1 < 2 and 3 > 2
.
What I'm getting at is that the proper way to be manipulating your XML input is to use libraries which "understand" the XML markup. For VBA you could use MSXML. Info can be found here: https://learn.microsoft.com/en-us/previous-versions/windows/desktop/ms763742(v=vs.85)?redirectedfrom=MSDN
Some options supported by MSXML you have for deleting certain nodes or doing text replacements:
The main point is that all the above will make sure you're outputting well-formed XML and these technologies are intended for reading and manipulating XML documents. I suggest you check out MSXML and do some playing around with these APIs and a simple input document to get a feel for what's possible and what you prefer.
Upvotes: 1