views:

741

answers:

6

Is it possible to write a REGEX (search replace) that when run on an XML string will output that XML string indented nicely?

If so whats the REGEX :)

+4  A: 

Doing this would be far, far simpler if you didn't use a regex. In fact I'm not even sure it's possible with regex.

Most languages have XML libraries that would make this task very simple. What language are you using?

Greg
+1  A: 

This would only be acheivable with multiple regexs, which will perform like a state machine.

What you are looking for is far better suited to an off the cuff parser.

DevelopingChris
A single regex is already a state machine. I'm pretty sure that this would be possible with a regex (provided lookahead is supported) but the result would be pretty darn ugly.
Drew Noakes
+2  A: 

I don't know if a regex, in isolation, could do a pretty-print format of an arbitrary XML input. You would need a regex being applied by a program to find a tag, locate the matching closing tags (if the tag is not self-closed), and so on. Using regex to solve this problem is really using the wrong tool for the job. The simplest possible way to pretty print XML is to use an XML parser, read it in, set appropriate serialization options, and then serialize the XML back out.

Why do you want to use regex to solve this problem?

Eddie
+2  A: 

Using a regex for this will be a nightmare. Keeping track of the indentation level based on the hierarchy of the nodes will be almost impossible. Perhaps perl's 5.10 regular expression engine might help since it's now reentrant. But let's not go into that road... Besides you will need to take into account CDATA sections which can embed XML declarations that need to be ignored by the indentation and preserved intact.

Stick with DOM. As it was suggested in the other answer, some libraries provide already a function that will indent a DOM tree for you. If not building one will be much simplier than creating and maintaining the regexes that will do the same task.

potyl
+4  A: 

Is it possible to write a REGEX (search replace) that when run on an XML string [...anything]

No.

Use an XML parser to read the string, then an XML serialiser to write it back out in ‘pretty’ mode.

Each XML processor has its own options so it depends on platform, but here is the somewhat long-winded way that works on DOM Level 3 LS-compliant implementations:

input= implementation.createLSInput();
input.stringData= unprettyxml;
parser= implementation.createLSParser(implementation.MODE_SYNCHRONOUS, null);
document= parser.parse(input);
serializer= implementation.createLSSerializer();
serializer.domConfig.setParameter("format-pretty-print", true);
prettyxml= serializer.writeToString(document);
bobince
If it were possible to actually yell online, I would yell the first two sentences of this post a lot.
Robert Rossney
+1  A: 

The dark voodoo regexp as described here works great.
http://www.perlmonks.org/?node_id=261292
Its main advantage against using XML::LibXMl and others is that it's an order of magnitude faster.

Zorglub