我正在使用XML数据进行硬件分配,但我不知道如何判断XML是否有效。到目前为止这是我的代码。我想我应该解析XML,但对如何验证它感到困惑。
//XML Data Sets:
http://aiweb.cs.washington.edu/research/projects/xmltk/xmldata/www/repository.html
//standard includes
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import os
%matplotlib inline
//import the lxml parser
from lxml import etree
dtd = etree.DTD('ebay.dtd')
ebay = etree.parse('ebay.xml')
答案 0 :(得分:1)
lxml
会在要求解析格式错误的XML时抛出异常lxml.etree.XMLSyntaxError
。
所以你可以这样处理:
xml_fname = 'ebay.xml'
try:
ebay = etree.parse(xml_fname)
except etree.XMLSyntaxError:
print "Failed to parse an invalid XML: " + xml_fname
如果您想验证DTD:
xml = etree.XML(etree.tostring(ebay))
print(dtd.validate(xml))