Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

format an xml file with no root node in linux

I tried to format the below XML using xmllint --format xmlfilename with no luck since the file does not have a root node.

As a test I did put a root node and the XML file was formatted successfully.

How can I format this file without a root node?

<connection_param>
    <mlc_props_file>
        <file_full_path>./fs/public/mxres/mxmlc/mlc_properties.mxres</file_full_path>
    </mlc_props_file>
</connection_param>

<login group="CONFIG" password="00100020004100470016" user="CONFIG"/>
like image 575
mkazma Avatar asked Feb 18 '26 13:02

mkazma


2 Answers

You can incorporate that content into a "wrapper" XML file that includes it's content using an external entity reference.

In the example below, it assumes that your content is the fragment.xml file and the "wrapper" XML is in the same directory.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE wrapper [
  <!ENTITY content SYSTEM "fragment.xml">
]>
<wrapper>
    &content;
</wrapper>

Then parse/transform the "wrapper" XML file in order to parse/format the content.

like image 104
Mads Hansen Avatar answered Feb 21 '26 10:02

Mads Hansen


Use tidy:

echo '
<connection_param>
<mlc_props_file>
<file_full_path>./fs/public/mxres/mxmlc/mlc_properties.mxres</file_full_path>
</mlc_props_file>
</connection_param>
<login group="CONFIG" password="00100020004100470016" user="CONFIG"/>
'| tidy -xml -qi

Output:

<connection_param>
  <mlc_props_file>
    <file_full_path>
    ./fs/public/mxres/mxmlc/mlc_properties.mxres</file_full_path>
  </mlc_props_file>
</connection_param>
<login group="CONFIG" password="00100020004100470016"
user="CONFIG" />
like image 45
Marinos An Avatar answered Feb 21 '26 12:02

Marinos An



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!