2017-01-12 12 views
1

lxmlを使用してサブタグの内容を取得しようとしています。私が解析しているXMLファイルは有効ですが、何らかの理由で子要素を解析して解析すると、無効なXMLがあると思われます。私は他の投稿から、このエラーは通常閉じタグがないのにXMLがブラウザでうまく解析されたときに生成されることを見てきました。これがなぜ起こっているのか? XMLファイルのPython XML解析の子タグ

内容(のtest.xml):

<?xml version="1.0" encoding="UTF-8"?> 
<Group id="RHEL-07-010010"> 
    <title>SRG-OS-000257-GPOS-00098</title> 
    <description>&lt;GroupDescription&gt;&lt;/GroupDescription&gt; </description> 
    <Rule id="RHEL-07-010010_rule" severity="high" weight="10.0"> 
     <version>RHEL-07-010010</version> 
     <title>The file permissions, ownership, and group membership of system files and commands must match the vendor values.</title> 
     <description>&lt;VulnDiscussion&gt;Discretionary access control is weakened if a user or group has access permissions to system files and directories greater than the default. 

Satisfies: SRG-OS-000257-GPOS-00098, SRG-OS-000278- GPOS-00108&lt;/VulnDiscussion&gt;&lt;FalsePositives&gt;&lt; /FalsePositives&gt;&lt;FalseNegatives&gt;&lt; /FalseNegatives&gt;&lt;Documentable&gt;false&lt; /Documentable&gt;&lt;Mitigations&gt;&lt; /Mitigations&gt;&lt;SecurityOverrideGuidance&gt;&lt; /SecurityOverrideGuidance&gt;&lt;PotentialImpacts&gt;&lt; /PotentialImpacts&gt;&lt;ThirdPartyTools&gt;&lt; /ThirdPartyTools&gt;&lt;MitigationControl&gt;&lt; /MitigationControl&gt;&lt;Responsibility&gt;&lt; /Responsibility&gt;&lt;IAControls&gt;&lt;/IAControls&gt;</description> 
     <ident system="http://iase.disa.mil/cci">CCI-001494</ident> 
     <ident system="http://iase.disa.mil/cci">CCI-001496</ident> 
     <fixtext fixref="F-RHEL-07-010010_fix">Run the following command to determine which package owns the file: 

# rpm -qf &lt;filename&gt; 

Reset the permissions of files within a package with the following command: 

#rpm --setperms &lt;packagename&gt; 

Reset the user and group ownership of files within a package with the following command: 

#rpm --setugids &lt;packagename&gt;</fixtext> 
     <fix id="F-RHEL-07-010010_fix" /> 
     <check system="C-RHEL-07-010010_chk"> 
     <check-content-ref name="M" href="VMS_XCCDF_Benchmark_SRG.xml" /> 
      <check-content>Verify the file permissions, ownership, and group membership of system files and commands match the vendor values. 
Check the file permissions, ownership, and group membership of system files and commands with the following command: 

# rpm -Va | grep '^.M' 

If there is any output from the command, this is a finding.</check-content> 
     </check> 
    </Rule> 
    </Group> 

私はVulnDiscussionタグの内容を取得しようとしています。今、私がしようと、これまでのところは良い

<GroupDescription></GroupDescription> 
<VulnDiscussion>Discretionary access control is weakened if a user or group has access permissions to system files and directories greater than the default. 

Satisfies: SRG-OS-000257-GPOS-00098, SRG-OS-000278-GPOS-00108</VulnDiscussion> <FalsePositives></FalsePositives><FalseNegatives> </FalseNegatives><Documentable>false</Documentable><Mitigations></Mitigations> <SecurityOverrideGuidance></SecurityOverrideGuidance><PotentialImpacts> </PotentialImpacts><ThirdPartyTools></ThirdPartyTools><MitigationControl> </MitigationControl><Responsibility></Responsibility><IAControls></IAControls> 

、これでVulnDiscussionの内容を抽出します。これは、次の出力を生成

from lxml import etree as ET 

xml = ET.parse("test.xml") 
for description in xml.xpath('//description/text()'): 
print(description) 

を:私はこのような親タグ、議論の内容を取得することができますコード:

for description in xml.xpath('//description/text()'): 
    vulnDiscussion = next(iter(ET.XML(description).xpath('//VulnDiscussion/text()')), None) 
    print(vulnDiscussion) 

と、次のエラーを取得:

vulnDiscussion = next(iter(ET.XML(description).xpath('//VulnDiscussion/text()')), None) 
    File "src/lxml/lxml.etree.pyx", line 3192, in lxml.etree.XML (src/lxml/lxml.etree.c:78763) 
    File "src/lxml/parser.pxi", line 1848, in lxml.etree._parseMemoryDocument (src/lxml/lxml.etree.c:118341) 
    File "src/lxml/parser.pxi", line 1736, in lxml.etree._parseDoc (src/lxml/lxml.etree.c:117021) 
    File "src/lxml/parser.pxi", line 1102, in lxml.etree._BaseParser._parseDoc (src/lxml/lxml.etree.c:111265) 
    File "src/lxml/parser.pxi", line 595, in lxml.etree._ParserContext._handleParseResultDoc (src/lxml/lxml.etree.c:105109) 
    File "src/lxml/parser.pxi", line 706, in lxml.etree._handleParseResult (src/lxml/lxml.etree.c:106817) 
    File "src/lxml/parser.pxi", line 635, in lxml.etree._raiseParseError (src/lxml/lxml.etree.c:105671) 
    File "<string>", line 3 
lxml.etree.XMLSyntaxError: Extra content at the end of the document, line 3, column 79 

答えて

1

XMLは「ルート」を1つしか持てませんが、xml.xpath( '// description/text()')は複数の要素を返します。すべての要素を1つの要素にラップすると、XML文書にはルート要素が1つしかありません。

はまた、元のXML内のテキストを使用して、削除する必要があり、各終了タグの前にスペースがあることを指摘し

from lxml import etree as ET 

xml = ET.parse("test.xml") 

    for description in xml.xpath('//description/text()'): 
    x = ET.XML('<Testroot>'+description.replace('< /','</')+'</Testroot>') # add root tag and remove space before the closing tag 
    vulnDiscussion = next(iter(x.xpath('//VulnDiscussion/text()')), None) 
    if vulnDiscussion: 
     print(vulnDiscussion) 

出力

Discretionary access control is weakened if a user or group has access permissions to system files and directories greater than the default. 

    Satisfies: SRG-OS-000257-GPOS-00098, SRG-OS-000278- GPOS-00108 
+0

なぜ「なし」と、それはどのようにすることができouputをそれをしません削除? –

+0

タグが元のメッセージに2回表示され、最初にというサブエレメントがありません。このチェックを含めるようにスクリプトを更新しました – Shijo