title: Python :: How to split a xml string by tag?
body: <p>I have a string (in a file called token.txt) with the following text.</p>
  
  <pre><code>&lt;nexttoken&gt;test1&lt;/nexttoken&gt;
  &lt;nexttoken&gt;test2&lt;/nexttoken&gt;
  </code></pre>
  
  <p>I want to remove the tags <code>&lt;nexttoken&gt;</code> and I want to display the second line i.e. <code>test2</code> </p>
  
  <p>Output = test2</p>
  
  <p><strong>What I tried:</strong></p>
  
  <pre><code>with open("token.txt") as f:   
          for line in f:
              if "nexttoken" in line: 
                  lines_contain_next_token = line 
                  n2=lines_contain_next_token.replace("&lt;/nexttoken&gt;","\n")
                  n3=n2.replace("&lt;nexttoken&gt;","\n")
                  n4=n3.replace("\n",",")
                  n5=n4.replace(' ','')
                  print n5
  </code></pre>