• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
OpenTechTips

OpenTechTips

Comprehensive IT Guides for Pros and Enthusiasts

MENUMENU
  • HOME
  • ALL TOPICS
    • Exchange
    • InfoSec
    • Linux
    • Networking
    • Scripting
      • PowerShell
    • SSL
    • Tools
    • Virtualization
    • Web
    • Windows
  • ABOUT
  • SUBSCRIBE
Home » PowerShell: How to parse a large one-line XML

PowerShell: How to parse a large one-line XML

March 1, 2022 - by Zsolt Agoston - last edited on March 1, 2022

Command Syntax

# Parse unformatted xml files, adding line breaks and indentation
xmllint YOUR_FILE.xml --format > OUTPUT_FILE_NAME.xml

Details

Recently I have come across a huge (over 4GB large) XML file that I tried to open and simply nothing worked. Literally nothing.

I tried to open it in Notepad++, VS Code, even the VI editor but without any luck.

 

PowerShell: How to parse a large one-line XML

I either received a warning saying I've got not enough memory or devastatingly VI - that I had the most hope on - simply crashed!

Turned out the XML file contained no line breaks to all of the parsers run out of memory before they could load the whole thing.

So the fix might be adding line breaks in the file, even add indentations to make it prettier?

On one side this would make the XML file bigger, the other hand it would make editors easier to open the XML as it would be already parsed so they could go line by line instead of loading the whole thing and then parsing it.

Example

Here we parse our largeXML.xml file that gave us a hard time earlier. It's located in c:\temp, so we parse it with the following command after opening the Linux subsystem (for detailed steps on how to configure the Linux subsystem please click here). Simply open Run (Win + R), type in bash and hit Enter.

PowerShell: How to parse a large one-line XML

Next, navigate to the folder where the file is located in. In my case it's the temp directory on the C: drive, which is accessible as '/mnt/c/temp' in the linux subsystem.

Missing command!

If you receive an xmllint: command not found error, install the libxml2 package in Debian based systems:
sudo apt-get install libxml2-utils

Then run the xmllint command in the following format to add line breaks and indentation to the XML file:

# Navigate to C:\temp
cd /mnt/c/temp

# Parse the XML, saving it as largeXML_parsed.xml
xmllint largeXML.xml --format > largeXML_parsed.xml

Verification

After parsing VIM has no isues opening the file.

PowerShell: How to parse a large one-line XML

Reader Interactions

Comments Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Tools

Secondary Sidebar

CONTENTS

  • Command Syntax
  • Details
  • Example
  • Missing command!
  • Verification

  • Terms of Use
  • Disclaimer
  • Privacy Policy
Manage your privacy

To provide the best experiences, we and our partners use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us and our partners to process personal data such as browsing behavior or unique IDs on this site and show (non-) personalized ads. Not consenting or withdrawing consent, may adversely affect certain features and functions.

Click below to consent to the above or make granular choices. Your choices will be applied to this site only. You can change your settings at any time, including withdrawing your consent, by using the toggles on the Cookie Policy, or by clicking on the manage consent button at the bottom of the screen.

Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
Statistics

Marketing

Features
Always active

Always active
Manage options Manage services Manage {vendor_count} vendors Read more about these purposes
Manage options
{title} {title} {title}
Manage your privacy
To provide the best experiences, we use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us to process data such as browsing behavior or unique IDs on this site. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Functional Always active
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
Preferences
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
Statistics
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
Marketing
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
Statistics

Marketing

Features
Always active

Always active
Manage options Manage services Manage {vendor_count} vendors Read more about these purposes
Manage options
{title} {title} {title}