如何判断是否有效数据

时间:2018-02-28 16:11:45

标签: python xml

我正在使用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') 

1 个答案:

答案 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))