Jump to content

place xml closing tags on newline for empty nodes only


davidkierz

Recommended Posts

i need to replicate this strange dotnet behavior exactly.

 

<?xml version="1.0"?>
<library>
   <book>
       <Title>Green Eggs And ham</Title>
       <page>
       </page>
   </book>
</library>
 
nodes that are empty have the closing tag on a new, indented line. Nodes with content have the closing on the same line.
 
in php, i am saving my xml with $xml = $dom->saveXML($dom, LIBXML_NOEMPTYTAG) which pretty closely matches this, with the excepton of empty nodes having the closing tag on the same line
 
<?xml version="1.0"?>
<library>
   <book>
       <Title>Green Eggs And ham</Title>
       <page></page>
   </book>
</library>

i dont see option for placing closing tags for empty nodes on new lines, any ideas how i can go about recreating this behavior?

 

Link to comment
Share on other sites

Why do you have to replicate that? Putting the end tag on a newline changes the content: it's not an empty node anymore but a node with a bunch of whitespace as its value.

 

Whatever. Try regular expressions to replace "the beginning of the line, whitespace, and " with "the whitespace, , newline, the whitespace again, and ".

preg_replace('#^(\s+)$#m', '$1\n$1', $xml)
Link to comment
Share on other sites

okay i think what ill do it break the string into an array of lines

check each line to see if its empty

if it is remove the closing tag, count how many indents, insert new indented line into array with closing tag

 

EDIT

the xml is encoded into binary and encrypted, signed, and hashed i need to create the exact same output. this is the only difference.

there are thousands of different tags that may or may not be empty, my example is only a simplification
Edited by davidkierz
Link to comment
Share on other sites

    $linnewline = "\n";
    $emptyline = "></";
    $lines = explode($linnewline,$xml);
    unset($xml);
    $xml = "";
    foreach ($lines as $line) {
        if (strpos($line,$emptyline) !== false) {
            $spaces = 0;
            for ($i = 0; $i<strlen($line); $i++){
                if ($line[$i] != " "){
                    break;
                }
                $spaces=$spaces+1;
            }
            $split = explode($emptyline, $line);
            $newline = $split[0] . ">\n" . str_repeat(" ", $spaces) . "</" . $split[1];
            $xml = $xml . $newline . $linnewline;
        } else {
            $xml = $xml . $line . $linnewline;
        }
    }

ugh.. its ugly and slow. but it works..

Edited by davidkierz
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.