This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Heracles421's profile picture

Getting a single line of code from an XML file?

Started by Heracles421, 06 January 2013 - 01:37 PM
Heracles421 #1
Posted 06 January 2013 - 02:37 PM
Spoiler

<?xml version='1.0' encoding='UTF-8'?>
<queryresult success='true'
	error='false'
	numpods='2'
	datatypes=''
	timedout=''
	timedoutpods=''
	timing='0.369'
	parsetiming='0.106'
	parsetimedout='false'
	recalculate=''
	id='MSPa1061a4f18h2c56b4i8900005gb016ai4b0bf6ba'
	auth=''
	host='http://www4a.wolframalpha.com'
	server='9'
	related='http://www4a.wolframalpha.com/api/v2/relatedQueries.jsp?id=MSPa1071a4f18h2c56b4i8900001250393228bd09eg&amp;s=9'
	version='2.6'>
<pod title='Input interpretation'
	 scanner='Identity'
	 id='Input'
	 position='100'
	 error='false'
	 numsubpods='1'>
  <subpod title=''>
  [color=#808080] [size=6]<plaintext>Hello.</plaintext>[/size][/color]
   <img src='http://www4a.wolframalpha.com/Calculate/MSP/MSP1081a4f18h2c56b4i89000027531ad46098iaic?MSPStoreType=image/gif&amp;s=9'
	   alt='Hello.'
	   title='Hello.'
	   width='38'
	   height='18' />
  </subpod>
</pod>
<pod title='Response'
	 scanner='Data'
	 id='Result'
	 position='200'
	 error='false'
	 numsubpods='1'
	 primary='true'>
  <subpod title=''>
  [color=#808080][size=6] <plaintext>Hello, human.</plaintext>[/size][/color]
   <img src='http://www4a.wolframalpha.com/Calculate/MSP/MSP1091a4f18h2c56b4i890000438g954i79edac6g?MSPStoreType=image/gif&amp;s=9'
	   alt='Hello, human.'
	   title='Hello, human.'
	   width='94'
	   height='18' />
  </subpod>
</pod>
<assumptions count='1'>
  <assumption type='Clash'
	  word='Hi'
	  template='Assuming &amp;quot;${word}&amp;quot; is ${desc1}. Use as ${desc2} instead'
	  count='2'>
   <value name='Miscellaneous'
	   desc='a phrase'
	   input='*C.Hi-_*Miscellaneous-' />
   <value name='Chemical'
	   desc='a chemical compound'
	   input='*C.Hi-_*Chemical-' />
  </assumption>
</assumptions>
</queryresult>

How can I separate the lines of code surrounded by the <plaintext> tags from everything else? And is it possible to erase the <plaintext>&amp;</plaintext> tags to keep just the answer?
Kingdaro #2
Posted 06 January 2013 - 02:48 PM
Would string.match work? Or string.gmatch if you want all of the <plaintext>s.


local document = 'that XML stuff you have there'
local texts = {}

for content in document:gmatch "<plaintext>(.+)</plaintext>" do
  table.insert(texts, content)
end

Basically throws the content of all of the plaintext tags into the "texts" table.</code>

</div></plaintext></code>

</div></plaintext>
Heracles421 #3
Posted 06 January 2013 - 03:28 PM
Would string.match work? Or string.gmatch if you want all of the <plaintext>s.


local document = 'that XML stuff you have there'
local texts = {}

for content in document:gmatch "<plaintext>(.+)</plaintext>" do
  table.insert(texts, content)
end

Basically throws the content of all of the plaintext tags into the "texts" table.</code>

</div></plaintext></code>

</div></plaintext>
Almost worked, the thing that happens is: It looks from the first <plaintext> to the last </plaintext>, instead of looking from <plaintext> to the next </plaintext>
Kingdaro #4
Posted 06 January 2013 - 03:32 PM
Oops.

Change "(.+)" to "(.-)".