Closing open XML tags with regular expressions

Basically, I want to do the same thing here that runs in Python. I would like to replace all self-closing elements with a long syntax.

Example

    <iframe src="http://example.com/thing"/>

becomes

    <iframe src="http://example.com/thing"></iframe>

Full example:

 <html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <link rel="stylesheet" type="text/css" href="/sample.css">
  <title></title>
  <script type="text/javascript" src="/swfobject.js">
                //void
          </script>
  <script type="text/javascript" language="JavaScript" src="/generate.js">
//void
  </script>
  <script type="text/javascript" language="JavaScript" src="/prototype.js">
//void
  </script>
</head>
<body id="mediaPlayer" style="margin:0;padding:0;">
<script type="text/javascript">
                                swfobject.registerObject('id_G12564763');       


                function getFlashObject() {
                        var object;
                        if (navigator.appName == 'Microsoft Internet Explorer' || navigator.userAgent.indexOf("Chrome")!=-1)
                        {
                                object = document.getElementById('id_G12564763');
                        } 
                        else 
                        {
                                object = document['flash_id_G12564763'];
                        }
                        return object;
                }

        </script>
</body>
</html>
+5
source share
3 answers

Ok guys. I found a workaround. I hooked the output method to xml, where does this html come from, and the XSLT engine will take care of closing these open tags for me. Thanks for the answers, but if you have a solution to the problem with PLS, leave your answer and I will mark it as the answer. This may be helpful to others.

+1
source

( javascript).

var becomes = "<iframe src='http://example.com/thing'/>".replace(/<(\w*) (.*)\//,'<$1 $2></$1')

, Java.

String becomes = "<iframe src=\"http://example.com/thing\"/>".replaceFirst("<(\\w*) (.*)\\/", "<$1 $2></$1");
+1
String resultHtml = inputHtml.replaceAll("(?six)<(\\w+)([^<]*?)/>", "<$1$2></$1>");

and it will correctly handle tags that do not terminate both <hr>and<img>

+1
source

All Articles