1

Python 2.7.2を実行するXML処理(lxmlを使用)の基本を学ぼうとしています。本当に単純な開始ファイルを作成しましたが、それはクレーターです。コードは次のとおりです。

from lxml import etree

doc = etree.parse('/Desktop/plc_dmt.xml')

print doc

さまざまなxmlファイルを使用し、etree.parse()メソッドを実行する前に最初にファイルを開くことで、このコードのバリエーションを試しましたが、以下のような類似または同一のエラーメッセージが表示されます。

Traceback (most recent call last):
  File "XMLparse_test.py", line 7, in <module>
    doc = etree.parse('/Users/Dad/Desktop/plc_dmt.xml')
  File "lxml.etree.pyx", line 2954, in lxml.etree.parse (src/lxml/lxml.etree.c:56220)
 ...   {Misc  error stuff}
 ... 
  lxml.etree.XMLSyntaxError: xmlParsePI : no target name, line 3, column 14

少なくとも一部のXMLファイルは、少なくともWebサーバーで正しく実行されている限り、整形式であることを確認しました。エラーメッセージがわかりません-探しているターゲット名は何ですか?

これが入力xmlファイルです。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<response>
<heartbeat><?--#exec cmd_argument='printf( "0x%02X%02X", InReadUByte( 0 ), InReadUByte( 1 ))'-->    </heartbeat>

<dmt node="1">
    <address><?--#exec cmd_argument='printf( "0x%02X", InReadUByte( 20 ))'--></address>
    <status><?--#exec cmd_argument='printf( "0x%02X", InReadUByte( 21 ))'--></status>
    <realflow><?--#exec cmd_argument='printf( "%f", InReadFloat( 22 ))'--></realflow>
    <pressure><?--#exec cmd_argument='printf( "0x%02X%02X", InReadUByte( 26 ), InReadUByte( 27 ))'--></pressure>
    <temp><?--#exec cmd_argument='printf( "0x%02X%02X", InReadUByte( 28 ), InReadUByte( 29 ))'--></temp>
</dmt>
# Misc stuff pulled out to keep file shorter...
</response>

埋め込まれたコードの多くは、このWebサーバーのサーバーサイドインクルードコマンドであり、一部のインストルメンテーションに接続されています。このファイルはサーバー上で正しく動作します。

4

1 に答える 1

0

要素内に文字が含まれ<ているため、XMLは無効です。>彼らは逃げなければなりません。

それらが実際にコメントであるはずだった場合、これはそれがどうあるべきかです:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<response>
<heartbeat><!--#exec cmd_argument='printf( "0x%02X%02X", InReadUByte( 0 ), InReadUByte( 1 ))'-->    </heartbeat>

<dmt node="1">
    <address><!--#exec cmd_argument='printf( "0x%02X", InReadUByte( 20 ))'--></address>
    <status><!--#exec cmd_argument='printf( "0x%02X", InReadUByte( 21 ))'--></status>
    <realflow><!--#exec cmd_argument='printf( "%f", InReadFloat( 22 ))'--></realflow>
    <pressure><!--#exec cmd_argument='printf( "0x%02X%02X", InReadUByte( 26 ), InReadUByte( 27 ))'--></pressure>
    <temp><!--#exec cmd_argument='printf( "0x%02X%02X", InReadUByte( 28 ), InReadUByte( 29 ))'--></temp>
</dmt>
</response>

それが実際にテキストであると想定されていた場合は、次のようにエスケープする必要があります。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<response>
<heartbeat>&lt;?--#exec cmd_argument='printf( "0x%02X%02X", InReadUByte( 0 ), InReadUByte( 1 ))'--&gt;    </heartbeat>

<dmt node="1">
    <address>&lt;?--#exec cmd_argument='printf( "0x%02X", InReadUByte( 20 ))'--&gt;</address>
    <status>&lt;?--#exec cmd_argument='printf( "0x%02X", InReadUByte( 21 ))'--&gt;</status>
    <realflow>&lt;?--#exec cmd_argument='printf( "%f", InReadFloat( 22 ))'--&gt;</realflow>
    <pressure>&lt;?--#exec cmd_argument='printf( "0x%02X%02X", InReadUByte( 26 ), InReadUByte( 27 ))'--&gt;</pressure>
    <temp>&lt;?--#exec cmd_argument='printf( "0x%02X%02X", InReadUByte( 28 ), InReadUByte( 29 ))'--&gt;</temp>
</dmt>
</response>

上記の2つの文書は両方とも有効です。

于 2012-08-04T19:51:03.733 に答える