python读取xml文件时的问题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python读取xml文件时的问题相关的知识,希望对你有一定的参考价值。

py文件
#!/usr/bin/env python
#coding=utf-8
from xml.dom.minidom import parse, parseString

def getText(nodelist):
rc =''
for node in nodelist:
if node.nodeType == node.TEXT_NODE:
rc = rc + node.data
return rc

def readConfig(filename):
dom1 = parse(filename) # parse an XML file by name

config_element = dom1.getElementsByTagName('config')[0]
routelist = config_element.getElementsByTagName('routelist')[0]
route = config_element.getElementsByTagName('route')[0]
ip = config_element.getElementsByTagName('ip')[0]
port = config_element.getElementsByTagName('port')[0]
delay = config_element.getElementsByTagName('delay')[0]

return (getText(routelist.childNodes),getText(route.childNodes),getText(ip.childNodes),getText(port.childNodes),getText(delay.childNodes))

str1,str2,str3,str4,str5 = readConfig('xf.xml')
print str1,str2,str3,str4,str5

xml文件
<?xml version=”1.0″ encoding=”utf-8″?>
<config>
<routelist>routelist</routelist>
<route>route</route>
<ip>ip</ip>
<port>port</port>
<delay>delay</delay>
</config>

enca命令查看xml文件的编码格式:
Universal transformation format 8 bits; UTF-8

然后python rc.py
出现问题:
Traceback (most recent call last):
File "rc.py", line 25, in <module>
str1,str2,str3,str4,str5 = readConfig('xf.xml')
File "rc.py", line 13, in readConfig
dom1 = parse(filename) # parse an XML file by name
File "/usr/lib/python2.6/xml/dom/minidom.py", line 1918, in parse
return expatbuilder.parse(file)
File "/usr/lib/python2.6/xml/dom/expatbuilder.py", line 924, in parse
result = builder.parseFile(fp)
File "/usr/lib/python2.6/xml/dom/expatbuilder.py", line 207, in parseFile
parser.Parse(buffer, 0)
xml.parsers.expat.ExpatError: XML declaration not well-formed: line 1, column 14
我三个地方都统一用了UTF-8啊。。。。怎么还出这样的错误啊。。。。真是无语。。。。。。。

参考技术A 把这行的引号都换成英文的。
<?xml version=”1.0″ encoding=”utf-8″?>本回答被提问者采纳

Java:Spring:读取spring context.xml时的XML冲突重复定义:'identifiedType'

【中文标题】Java:Spring:读取spring context.xml时的XML冲突重复定义:\'identifiedType\'【英文标题】:Java:Spring: XML conflict on reading spring context.xml Duplicated definition for: 'identifiedType'Java:Spring:读取spring context.xml时的XML冲突重复定义:'identifiedType' 【发布时间】:2014-10-13 04:27:56 【问题描述】:

非常感谢任何建议或帮助。

参考链接很多,但没有明确的解决方案。我想知道 Spring 项目如何与返回 XMLType 输出的 PLSQL 过程交互。您将需要 XMLParser,然后在与 Spring 一起使用时,我们都会收到此错误。有人有什么建议吗?

我有一个运行良好的 Maven spring 应用程序(mvc/Rest),我添加了以下依赖项以与 PLSQL 程序交互,

    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>ojdbc6</artifactId>
        <version>11.2.0.4</version>
    </dependency>
    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>xdb6</artifactId>
        <version>11.2.0.4</version>
    </dependency>
    <dependency>
        <groupId>com.xmlparserv2</groupId>
        <artifactId>xmlparserv2</artifactId>
        <version>11.1.1</version>
    </dependency>

包含xdb6xmlparserv2 后,我在Spring 应用程序启动时收到以下错误(运行服务器时)

2014-08-19 18:28:51,898 信息 |主要 | os.beans.factory.xml.XmlBeanDefinitionReader

Loading XML bean definitions from class path resource [META-INF/spring/myapp-context.xml] 


<Line 43, Column 57>: XML-24509: (Error) Duplicated definition for: 'identifiedType'
<Line 60, Column 28>: XML-24509: (Error) Duplicated definition for: 'beans'
<Line 140, Column 34>: XML-24509: (Error) Duplicated definition for: 'description'
<Line 152, Column 29>: XML-24509: (Error) Duplicated definition for: 'import'
<Line 174, Column 28>: XML-24509: (Error) Duplicated definition for: 'alias'
<Line 203, Column 33>: XML-24509: (Error) Duplicated definition for: 'beanElements'
<Line 218, Column 44>: XML-24509: (Error) Duplicated definition for: 'beanAttributes'
<Line 462, Column 43>: XML-24509: (Error) Duplicated definition for: 'meta'
<Line 470, Column 35>: XML-24509: (Error) Duplicated definition for: 'metaType'
<Line 487, Column 27>: XML-24509: (Error) Duplicated definition for: 'bean'
<Line 507, Column 38>: XML-24509: (Error) Duplicated definition for: 'constructor-arg'
.....

2014-08-19 18:28:52,014 ERROR | main | org.springframework.web.servlet.DispatcherServlet       | Context initialization failed 
org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 16 in XML document from class path resource [META-INF/spring/myapp-context.xml] is invalid; nested exception is oracle.xml.parser.schema.XSDException: Duplicated definition for: 'identifiedType'
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:396) ~[spring-beans-3.2.0.RELEASE.jar:3.2.0.RELEASE]
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334) ~[spring-beans-3.2.0.RELEASE.jar:3.2.0.RELEASE]
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302) ~[spring-beans-3.2.0.RELEASE.jar:3.2.0.RELEASE]

来自类路径资源 [META-INF/spring/myapp-context.xml] 的 XML 文档中的第 16 行无效;

myapp-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:sec="http://www.springframework.org/schema/security" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
    xmlns:cache="http://www.springframework.org/schema/cache"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd  
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/security
        http://www.springframework.org/schema/security/spring-security-3.1.xsd
        http://www.springframework.org/schema/util
        http://www.springframework.org/schema/util/spring-util-3.2.xsd
        http://www.springframework.org/schema/cache 
        http://www.springframework.org/schema/cache/spring-cache.xsd">

第 16 行是http://www.springframework.org/schema/cache/spring-cache.xsd"&gt;

假设我删除 Line16 及其引用,然后在不同的行引发相同的错误,比如第 13 行

更多错误堆栈跟踪:

Caused by: org.xml.sax.SAXParseException: <Line 13, Column 70>: XML-24500: (Error) Can not build schema 'http://www.springframework.org/schema/tx' located at 'http://www.springframework.org/schema/tx/spring-tx.xsd'
    at oracle.xml.parser.v2.XMLError.flushErrorHandler(XMLError.java:425) ~[xmlparserv2-11.1.1.jar:na]
    at oracle.xml.parser.v2.XMLError.flushErrors1(XMLError.java:287) ~[xmlparserv2-11.1.1.jar:na]
    at oracle.xml.parser.v2.NonValidatingParser.parseDocument(NonValidatingParser.java:343) ~[xmlparserv2-11.1.1.jar:na]
    at oracle.xml.parser.v2.XMLParser.parse(XMLParser.java:226) ~[xmlparserv2-11.1.1.jar:na]
    at oracle.xml.jaxp.JXDocumentBuilder.parse(JXDocumentBuilder.java:155) ~[xmlparserv2-11.1.1.jar:na]


Caused by: oracle.xml.parser.schema.XSDException: Duplicated definition for: 'identifiedType'
    at oracle.xml.parser.schema.XSDBuilder.buildSchema(XSDBuilder.java:794) ~[xmlparserv2-11.1.1.jar:na]
    at oracle.xml.parser.schema.XSDBuilder.build(XSDBuilder.java:489) ~[xmlparserv2-11.1.1.jar:na]
    at oracle.xml.parser.schema.XSDValidator.processSchemaLocation(XSDValidator.java:999) ~[xmlparserv2-11.1.1.jar:na]
    at oracle.xml.parser.schema.XSDValidator.startElement(XSDValidator.java:604) ~[xmlparserv2-11.1.1.jar:na]

更新 1

根据发布的答案,它说有一个快速的解决方案,

要覆盖 xmlparserv2.jar 所做的更改,请将以下 JVM 属性添加到您的应用程序服务器启动参数中。 java.xml.parsers.FactoryFinder 逻辑会先检查环境变量。

-Djavax.xml.parsers.SAXParserFactory=com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl

 -Djavax.xml.parsers.DocumentBuilderFactory=com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl

 -Djavax.xml.transform.TransformerFactory=com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl

不建议这样做,链接中明确说明 https://community.oracle.com/thread/1080787

这里提到了另一种解决方案(与hibernate有关,但类似):Using Oracle XMLType column in hibernate

在 maven spring 项目中,通过在 %PROJECT_HOME%/src/main/resources 目录中创建以下文件来覆盖 xmlparserv2.jar 设置:

%PROJECT_HOME%/src/main/resources/META-INF/services/javax.xml.parsers.DocumentBuilderFactory (which defines com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl as the default)
%PROJECT_HOME%/src/main/resources/META-INF/services/javax.xml.parsers.SAXParserFactory (which defines com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl as the default)
%PROJECT_HOME%/src/main/resources/META-INF/services/javax.xml.transform.TransformerFactory (which defines com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl as the default)

这对我来说不是很清楚。

如何将这些文件放到上述目录中?这是推荐的解决方案吗?

更新 2

我尝试添加 VM 参数 -Djavax.xml.parsers.DocumentBuilderFactory=com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl 并且它有效。

但是正在寻找替代的、更合适的解决方案。这只是一种解决方法。当应用程序通过 Jenkins 部署到服务器时,设置这些与 XML 解析器相关的 VM 参数设置是不合适的。

更新 3

@Ian Roberts 的回答解决了这个问题。我清理了 context.xml 以始终使用 xsd 的版本号。

【问题讨论】:

好消息@Ian Roberts。我正在清理它。当我看到你的评论时我很高兴:) 我已将我的评论变成您可以接受的答案。 【参考方案1】:

这篇文章已经很老了,但无论如何我今天遇到了这个问题并找到了原因和不同的解决方案。 事实证明,当您使用 Oracle 的 maven 存储库通过 maven 引入 oracle jdbc 驱动程序依赖项时,它会传递获取其他库,从而在解析 XML 时导致此问题。 我只是排除了 oracle 驱动程序的传递依赖,一切正常。 我认为在运行时根本不需要那些额外的库。

如果你的 pom 中有这个:

<dependency>
    <groupId>com.oracle.jdbc</groupId>
    <artifactId>ojdbc7</artifactId>
    <version>12.1.0.2</version>
</dependency>

这是依赖树:

ojdbc7:12.1.0.2 xdb6:12.1.0.2 orai18n : 12.1.0.2 xmlparserv2 : 12.1.0.2 oraclepki:12.1.0.2 osdt_cert:12.1.0.2 osdt_core:12.1.0.2

所以你可以像这样排除有问题的库:

<dependency>
    <groupId>com.oracle.jdbc</groupId>
    <artifactId>ojdbc7</artifactId>
    <version>12.1.0.2</version>
    <exclusions>
        <exclusion>
            <artifactId>xmlparserv2</artifactId>
            <groupId>com.oracle.jdbc</groupId>
        </exclusion>
    </exclusions>
</dependency>

我实际上排除了所有的传递依赖,一切似乎都很好。我不确定它们是否真的被使用过。我不这么认为,因为手动包含 jdbc 库的方式是简单地下载 ojdbc jar。

【讨论】:

ojdbc7 的 pom 声明:“这些文件是可选的,用于特定功能”。我什至会排除所有这些 可选 依赖项,因为它们也提供其他 META-INF/服务。【参考方案2】:

Here 有你的解决方案。

-Djavax.xml.parsers.DocumentBuilderFactory=com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl`

【讨论】:

谢谢,但这不是推荐的解决方案。这是为了将 JVM 属性添加到我的应用程序服务器启动参数中。 +1 供参考。但是花了一些时间才意识到“这里”是一个链接:) 感谢@Xstian,这也很有效,而且是中间缓解,而且不言自明,但与设置 VM 参数相比,我需要一个更通用的解决方案。 谢谢,我明白了。至少当它起作用时,它给我带来了微笑:) 并感谢您的链接 感谢@xstian ..如果没有混合模式,那么这个解决方案就完美了..绕过【参考方案3】:

您的 schemaLocation 是版本化 (spring-beans-3.2.xsd) 和非版本化 (spring-tx.xsd) 架构名称的混合体。您应该始终使用一种风格或另一种风格,而不是混合使用两种风格。我怀疑这里发生的事情是您明确引用了 spring-beans 3.2 版,但是您的一个未版本化架构导入了同一架构的不同版本,导致 beans:identifiedType 的两个定义冲突。

【讨论】:

这确实是答案。我不敢相信。没有更多的 XML 解析错误,所有与 XML 解析相关的参考链接 [来自 Oracle 论坛,SO 帖子],VM 参数没有什么比这更完美了。感谢您阅读我的原始帖子。现在,它的魔力,直到我了解 XML 解析错误是如何“神奇地”修复的!

以上是关于python读取xml文件时的问题的主要内容,如果未能解决你的问题,请参考以下文章

尝试使用Python 3读取UTF-8 XML文件

python读取,写入和更新xml文件

使用 Python 直接从 zip 文件中读取 xml 文件

python 读取xml文件

python3 读取XML文件的入坑经历

Python爬虫编程思想(76): 读取与搜索XML文件