Seems to me that XML::Twig
's xml_pp
utility does almost what you want with its indented_a
option:
$ xml_pp -s indented_a foo.xml
<!DOCTYPE parent [
<!ENTITY entity1 "value">
]>
<main>
<parent
attr1="str1"
attr2="str2">
<child
childattr1="str3"
childattr2="&entity1;"
/>
<child
childattr1="str4"
childattr2="&entity1;"
/>
</parent>
</main>