Link to home
Start Free TrialLog in
Avatar of Loco4e
Loco4eFlag for United States of America

asked on

Script to add a X-header in multiple .XML files at the beginning

I need to add X-header at the beginning of multiple "XML" files.

The X-header would be:

"X-Header Message Type: IM"

The XML files are going to located on a desktop folder name "XML FILES".

It should ignore the XML file name just open every file and add "X-Header Message Type: IM" at the beginning.
Avatar of ste5an
ste5an
Flag of Germany image

I need to add X-header at the beginning of multiple "XML" files
hmm, without context..

Are you talking about MIME and e-mail attachments or HTTP requests and content types?
XML files do not have headers like that. If you put a header like that into the file:

X-Header Message Type: IM

<xml blah blah>
....

Open in new window


...it will make the XML file invalid. Headers are part of the DELIVERY of the file. So for example, when you download a file from a web server, the web server responds with a block of HTTP headers first:
User generated image
The yellow portion is generated by the server (and possibly manipulated by a server-side language like ASP or PHP), then there's ONE blank line (which indicates the end of the headers and the body is about to begin), and then the body (highlighted in cyan).

So if you added an X-Header to the top of the file, it would be treated like part of the XML data when being downloaded - it would not be treated as a HTTP header.

Headers are not preserved anywhere - once the file is downloaded, you are left with the data and that's it. That's the intended way that things work. Headers only exist as instructions on how the file data should be handled during download.

If you're talking about an email header, it's the same thing - all headers are up at the beginning together, then there's a blank line and then the body of the message (and the body format will vary sometimes, but there's no mail headers to add):
User generated image
Those headers are generated from the email client when the email is sent out, and then they are usually appended with more headers from the servers that relay the message around.

Either way, you don't store headers in the XML file themselves - you specify them during delivery.
Also a couple follow-up thoughts:

If you're going to specify a header, then it should not have any spaces. So this:
X-Header Message Type: IM
...would not work well, if at all (mail headers are notoriously less regulated, but there is no major browser that would properly interpret those spaces).

So it should be this:
X-Header-Message-Type: IM

Also, if you absolutely need to store some kind of indicator within the XML data itself (so it can be parsed by another client regardless of how the file was delivered), then you can simply put some identifier within a comment block that comes AFTER the <?xml....> declaration tag, like this:

BEFORE: <?xml version="1.0" encoding="utf-8"?><yourdata>...</yourdata>
AFTER: <?xml version="1.0" encoding="utf-8"?><!-- MESSAGE TYPE: IM --><yourdata>...</yourdata>

Or even better, if your XML structure supports indicating the type of data, then make it part of the data.
Avatar of Loco4e

ASKER

Sorry guys very sorry to my understating I need to attach the XML file to an email and send to our compliance.

But now they are telling me that I need to open each .XML file and copy the content into the body of an email.

So at the moment, I'm going to leave this question open until they tell what I actually need to do.

Thank you guys for helping me out.
A quick way to combine all the XML files into one big file that you can then just open and copy from, just run Powershell (I'm assuming you're running Windows since one of your topics is VBScript) and then once it's opened, run this one line:

 Get-ChildItem "C:\Users\YourUsername\Desktop" -Filter *.xml | Foreach-Object { Add-Content -Path "all_xml_files.txt" -Value (Get-Content $_.FullName) }

That should loop through all XML files in the C:\Users\YourUsername\Desktop folder and then append them all to one file called all_xml_files.txt.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.