Opening (parts of a) XML file in Windows command-line
Matthew Martinez
I captured some packets using tshark (wireshark) in command-line in Windows and then saved the file in .xml-format. Now I want to read that .xml-file in the command-line itself.
How to read an xml file in command line?
22 Answers
Assuming you are on windows, there are 2 methods you can use here depending on what you mean by "read that xml file in the command line itself"
Using Type
The command prompt knows a command called type which displays the content of a file to the display.
C:\>type filename.xml
<xml file> variable
</xml file>
C:\>_If the xml file is big, you can pipe it to the more command so you see a page at the time.
C:\>type filename.xml | moreStart the file and let windows open it with the default xml viewer
Another option is to make windows open the file with the default xml viewer. Lets say Internet Explorer is your default xml viewer, the following command will start Internet Explorer, open the XML file and switch to it. You click the x and you return to the commandline program (unless you switch to other applications)
The command to start the file in the default program is called start
C:\>start filename.xmlVerdict
I personally think start is the preferred mode, because the default xml viewer will have syntax highlighting, where the command window does not, but if the xml file is very small and you want to quickly look up a text, copy that and use it in a new command, then the type command will be preferred.
2Assuming you wish to parse the actual XML inside a SOAP-envelope/package, you may do that by using PowerShell - if you instead wish to parse the actual SOAP-data you will need to consume it via a worker in a higher language, I am not aware that this is possible via PowerShell.
Regarding parsing a SOAP-message as a XML-structure - effectively ignoring all SOAP-related nodes and instead get the actually embedded XML:
You need to know the node-name. For example in:
<?xml version="1.0" encoding="UTF-8"?> <soap:Envelope xmlns:soap="" soap:encodingStyle=""> <soap:Header> ... </soap:Header> <soap:Body> <encapsulatingNodeOfInterest> <childNodeOfInterest exampleAttribute="exampleValue"> exampleInnerContent </childNodeOfInterest> <childNodeOfInterestToo exampleAttribute="exampleValue"> exampleInnerContent </childNodeOfInterestToo> </encapsulatingNodeOfInterest> <soap:Fault> ... </soap:Fault> </soap:Body> </soap:Envelope>... the node to select would be
encapsulatingNodeOfInterest.Access the file by using the
-Path-argument and point it to the file:Select-Xml -Path ".\soap.xml"Add the
-XPath-argument to specify the node (encapsulatingNodeOfInterest) you wish to select:Select-Xml -Path ".\soap.xml" -XPath "//encapsulatingNodeOfInterest"... this will output the Node, Path and Pattern
Assuming you wish to iterate over all childNodes 'pipe' the
encapsulatingNodeOfInterest-node to aForEach-Object-loop accessing theInnerXml:Select-Xml -Path ".\soap.xml" -XPath "//encapsulatingNodeOfInterest" | ForEach-Object { $_.Node.InnerXml }... this will output all child-nodes inside the
encapsulatingNodeOfInterest-node, namelychildNodeOfInterestand 'childNodeOfInterestToo' in the given example.
Note: For examples of how to use Select-Xml access the help for the command with the -Examples-attribute:
Get-Help Select-Xml -Examples