-2

could any one show me how i can use JavaScript regular expression to change following xml structure ?

default xml

    <dict>
    <key>TITLE</key>
    <string>first</string>
    <key>URL</key>
    <string>http://someurl.com/1.m3u8</string>
    </dict>
    <dict>
    <key>TITLE</key>
    <string>Second</string>
    <key>URL</key>
    <string>http://someurl.com/2.m3u8</string>
    </dict>
    <dict>
    <key>TITLE</key>
    <string>Third</string>
    <key>URL</key>
    <string>http://someurl.com/3.m3u8</string>
    </dict>

convert to :

    <dict>
    <url>http://someurl.com/1.m3u8</url>
    <title>First</title>
    </dict>
    <dict>
    <url>http://someurl.com/2.m3u8</url>
    <title>Second</title>
    </dict>
    <dict>
    <url>http://someurl.com/3.m3u8</url>
    <title>Third</title>
    </dict>

Edit: i want to parse converted xml using this code:

<button type="button" onclick="loadXMLDoc()">crate table</button>
<br><br>
<table id="demo"></table>

<script>
function loadXMLDoc() {
  var xmlhttp = new XMLHttpRequest();
  xmlhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      myFunction(this);
    }
  };
  xmlhttp.open("GET", "myxml.xml", true);
  xmlhttp.send();
}
function myFunction(xml) {
  var i;
  var xmlDoc = xml.responseXML;
  var table="<tr><th>URL</th><th>Title</th></tr>";
  var x = xmlDoc.getElementsByTagName("dict");
  for (i = 0; i <x.length; i++) { 
    table += "<tr><td>" +
    x[i].getElementsByTagName("url")[0].childNodes[0].nodeValue +
    "</td><td>" +
    x[i].getElementsByTagName("title")[0].childNodes[0].nodeValue +
    "</td></tr>";
  }
  document.getElementById("demo").innerHTML = table;
}
</script>
user1788736
  • 2,399
  • 16
  • 54
  • 105
  • 5
    You can't, or rather, you shouldn't. That's not what regexp is for. Parse this and manipulate as XML, which is what it is. (Although this is not valid XML, since it has no root element.) –  Oct 07 '16 at 12:45
  • well i only know how to parse xml if it is in that converted format ! so only way is to convert it ! – user1788736 Oct 07 '16 at 12:47
  • Which converted format? You mean the format you showed as "convert to:"? If you don't know how to parse it, then learn how, or search for "parse XML javascript". Then you can manipulate with normal DOM routines such as `element.children` or `element.textContent`. –  Oct 07 '16 at 12:47
  • torazaburo see my edit code i want to be able to parse the xml using above code ! But my xml is not on correct format! How to parse it without converting it then ? – user1788736 Oct 07 '16 at 12:52
  • 1
    i am not parsing with regex i want to change structure of xml so it is parsable ! – user1788736 Oct 07 '16 at 13:13
  • 1
    If you don't want to use regexp, then please edit the title and first part of your post to state that. Anyway, what do you mean by "parse"? Do you mean "access"? "Parse" means to analyze the string of characters written in some language, XML in this case, to bring it into some kind of internal format (such as DOM) which can be manipulated and accessed. You don't need to change the format to parse it. You need to parse it to change the format. –  Oct 07 '16 at 13:38
  • i want to display the data on table that is my goal – user1788736 Oct 07 '16 at 13:42

1 Answers1

-1

You can do it with capturing groups. However, it's VERY hard to do that in one single regex, so let's use two regexes.

The following regex (where .+? is the actual title) will insert proper titles, when used with the replacement string <title>$1</title>:

<title>TITLE<\/title>\n<string>(.+?)<\/string>

Then, use the following regex with the replacement string <url>$1</url> to insert proper URLs (where https?:\/\/\S+? is the URL):

<key>URL<\/key>\n<string>(https?:\/\/\S+?)</string>

Note: The fact that what you're trying to do is doable with regex does not mean it's a good idea to parse arbitrary XML with it.

Community
  • 1
  • 1
dorukayhan
  • 1,435
  • 4
  • 20
  • 26
  • Do I have to escape the forward slashes in replacement strings? – dorukayhan Oct 07 '16 at 13:47
  • @dorukayhan thanks for helping me. could you tell me how to call aboe regex if i received the xml via ajax request in data variable ? – user1788736 Oct 07 '16 at 13:52
  • @torazaburo Parsing **a subset of XML/HTML** with regex doesn't wake up the One whose Name cannot be expressed in the Basic Multilingual Plane. – dorukayhan Oct 07 '16 at 13:52
  • @user1788736 I don't know how to do that, to be honest. – dorukayhan Oct 07 '16 at 13:53
  • 3
    It's wrong morally, ethically, technically, and practically. –  Oct 07 '16 at 13:55
  • @torazaburo What if I add a note saying that it's a bad idea to parse arbitrary XML? – dorukayhan Oct 07 '16 at 14:22
  • @dorukayhan so those regex gives me value of title and url ?How to call it then ? – user1788736 Oct 07 '16 at 14:28
  • [RegExes are perfectly fine to parse an XML document, if you know what you are going to do.](http://stackoverflow.com/a/4234491/1020526) @torazaburo – revo Oct 07 '16 at 15:26
  • 1
    @revo Great, now I know who to call when the space shuttle explodes because someone slipped an extra attribute into the XML that your regexp wasn't expecting. –  Oct 07 '16 at 17:17
  • Then whom was responsible for the explosion probably didn't have any idea about what he/she was going to do. @torazaburo – revo Oct 07 '16 at 17:46
  • @revo No, he/she didn't have any idea about what **someone else might** be going to do. –  Oct 07 '16 at 18:38
  • If you think such a terrible failure (explosion!) could get happen you really shouldn't go the RegEx way. But since you are free to choose, if you choose it then it means *you know what you are going to do*. I'm repeating this latter sentence, which seems you can't get it, to express how should someone deal with it otherwise there is no mean for me to say it. I'm not responsible for it if you purposely make a mess of Regular Expressions. @torazaburo – revo Oct 07 '16 at 20:02