Python 和 xml : Reading data from xml file

标签 python xml

我有一个简单的python,其中我定义了styledoc,如下

styledoc = libxml2.parseDoc("""
<xsl:stylesheet version='1.0'
  xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
  xmlns:foo='http://example.com/foo'
  xsl:exclude-result-prefixes='foo'>

  <xsl:param name='bar'>failure</xsl:param>
  <xsl:template match='/'>
    <article><xsl:value-of select='foo:foo($bar)'/></article>
  </xsl:template>
</xsl:stylesheet>
""")

我想要将所有数据包含在文件 read.xml 之类的内容

styledoc = libxml2.parseDoc("read.xml");

但它给了我一个错误“read”未定义。我犯了什么错误?

最佳答案

parseDoc 采用包含 XML 的字符串,如第一个示例所示。要解析文件,请使用 parseFile 代替:

styledoc = libxml2.parseFile("read.xml")

关于Python 和 xml : Reading data from xml file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4651569/

相关文章:

python - pip 不会安装 channels_redis

python - 我在绘制球体和曲线时遇到问题

xml - GWT - Module.gwt.xml - XML 验证警告

javax.xml.bind.UnmarshalException : unexpected element (uri :"", 本地 :"TestSubject")。预期元素是 <{}Test>

xml - 从复杂的 Oracle XMLTYPE 数据中进行选择和插入

c# - 将 XmlNodeList 转换为 List<string>

python - 'strsep' 导致 Linux 内核卡住

python - 如何比较 flask 中的两个密码

python - 如何找到运动物体轨迹中的冗余路径(子路径)?

java - 如何使用资源 jar 中分解出的 .xsd 文件来指定 JAXB 架构?