Generally, what I want to do is parsing xml from url. This is what I have done:
I write the xml code on html file enclosed on <'textarea'><'\textarea'> tag:
<textarea rows="1000" cols="200" style="border:none;"> <?xml version="1.0"?> <data> <gambar> <id>wcl01</id> <url>https://1.bp.blogspot.com/- j9yARC6mAuY/Xp4aUTxe6eI/AAAAAAAAAGA/NegvRkwYdVAXhnTsrWoXYcjAzsHfR6BOQCLcBGAsYHQ/s320/Konferensi%2BIIWAS%2Bdi%2BVietnam.jpg</url> </gambar> <gambar> <id>wcl02</id> <url>https://1.bp.blogspot.com/-aIkYkd3ePMY/XqDDsTMYMAI/AAAAAAAAAHA/QKZOQ8cPr_0LUfLNrYrA3w6gvNV-ao-QCLcBGAsYHQ/s320/Konferensi%2BAptikom%2Bdi%2BBandung%2B1.jpg</url> </gambar> </data> </textarea>
On the website, this is how it looks:
Then I parse the xml using this code:
from urllib.request import urlopen from xml.etree.ElementTree import parse from lxml import etree var_url = urlopen('https://imanparyudi.000webhostapp.com/gambar.html') xmldoc = parse(var_url) elem = etree.XML(xmldoc, parser=parser)
but I got this error:
File "<string>", line unknown ParseError: XML or text declaration not at start of entity: line 2, column 0
I assume that this error is caused by whitespace at the beginning of the xml code. So, I have tried to remove this whitespace using, first: etree.XMLParser(remove_blank_text=True) and second: etree.XMLParser(recover=True) like this:
from urllib.request import urlopen
from xml.etree.ElementTree import parse
from lxml import etree
parser = etree.XMLParser(remove_blank_text=True)
var_url = urlopen('https://imanparyudi.000webhostapp.com/gambar.html')
xmldoc = parse(var_url)
elem = etree.XML(xmldoc, parser=parser)
and
from urllib.request import urlopen
from xml.etree.ElementTree import parse
from lxml import etree
parser = etree.XMLParser(recover=True)
var_url = urlopen('https://imanparyudi.000webhostapp.com/gambar.html')
xmldoc = parse(var_url)
elem = etree.XML(xmldoc, parser=parser)
But, both ways give the same error:
File "<string>", line unknown ParseError: XML or text declaration not at start of entity: line 2, column 0
- So, my questions here are:
a. Is this problem cause by the use of <'textarea'><'\textarea'> tag?
b. If so, how can I post my xml code on a website?
c. If not, how can solve this ParseError?