使用Ruby和Nokogiri处理XML文件
我是编程新手,请容忍我。我有许多XML文档如下所示: 文件名:PRIDE_Exp_Complete_Ac_10094.xml.gz使用Ruby和Nokogiri处理XML文件,ruby,xml,rubygems,nokogiri,Ruby,Xml,Rubygems,Nokogiri,我是编程新手,请容忍我。我有许多XML文档如下所示: 文件名:PRIDE_Exp_Complete_Ac_10094.xml.gz <ExperimentCollection version="2.1"> <Experiment> <ExperimentAccession>1015</ExperimentAccession> <Title>Protein complexes in Saccharomyces cerevi
<ExperimentCollection version="2.1">
<Experiment>
<ExperimentAccession>1015</ExperimentAccession>
<Title>Protein complexes in Saccharomyces cerevisiae (GPM06600002310)</Title>
<ShortLabel>GPM06600002310</ShortLabel>
<Protocol>
<ProtocolName>None</ProtocolName>
</Protocol>
<mzData version="1.05" accessionNumber="1015">
<cvLookup cvLabel="RESID" fullName="RESID Database of Protein Modifications" version="0.0" address="http://www.ebi.ac.uk/RESID/" />
<cvLookup cvLabel="UNIMOD" fullName="UNIMOD Protein Modifications for Mass Spectrometry" version="0.0" address="http://www.unimod.org/" />
<description>
<admin>
<sampleName>GPM06600002310</sampleName>
<sampleDescription comment="Ho, Y., et al., Systematic identification of protein complexes in Saccharomyces cerevisiae by mass spectrometry. Nature. 2002 Jan 10;415(6868):180-3.">
<cvParam cvLabel="NEWT" accession="4932" name="Saccharomyces cerevisiae (Baker's yeast)" value="Saccharomyces cerevisiae" />
</sampleDescription>
</admin>
</description>
<spectrumList count="0" />
</mzData>
</Experiment>
有人能帮我吗
谢谢如果您对REXML感到满意,并且每个文件只有一个
,那么下面的内容应该会有所帮助。。。(顺便说一下,上面的文本是无效的XML,因为没有结束标记)
需要“rexml/document”
包括REXML
xml=如果您对REXML感到满意,并且每个文件只有一个
,那么下面的内容应该会有所帮助。。。(顺便说一下,上面的文本是无效的XML,因为没有结束标记)
需要“rexml/document”
包括REXML
xml=关于您在问题位置留下的“请为我删除”注释(在我将其回滚到问题之前),您可以使用问题文本下方的链接删除您自己的问题(您应该看到类似于:edit | close | delete
),如果您想删除它,您可以随意删除,既然你拥有这个问题。我把它退回去,因为它看起来是合法的,值得回答。如果您已经解决了问题,请发布您的解决方案。否则,请给它时间让人们看到它,并提供他们的帮助。关于您留下的代替您的问题的“请为我删除”评论(在我将其回滚到问题之前),您可以使用问题文本下方的链接删除您自己的问题(您应该看到类似于:edit | close | delete
)如果你想删除它,你可以这样做,因为你自己的问题。我把它退回去,因为它看起来是合法的,值得回答。如果您已经解决了问题,请发布您的解决方案。否则,给它时间让人们看到它,并提供他们的帮助。
require 'rubygems'
require 'nokogiri'
doc = Nokogiri::XML(File.open("PRIDE_Exp_Complete_Ac_10094.xml.gz"))
@ExperimentCollection = doc.css("ExperimentCollection Title").map {|node| node.children.text }
require "rexml/document"
include REXML
xml=<<EOD
<Experiment>
<ExperimentAccession>1015</ExperimentAccession>
<Title>Protein complexes in Saccharomyces cerevisiae (GPM06600002310)</Title>
<ShortLabel>GPM06600002310</ShortLabel>
<Protocol>
<ProtocolName>None</ProtocolName>
</Protocol>
<mzData version="1.05" accessionNumber="1015">
<cvLookup cvLabel="RESID" fullName="RESID Database of Protein Modifications" version="0.0" address="http://www.ebi.ac.uk/RESID/" />
<cvLookup cvLabel="UNIMOD" fullName="UNIMOD Protein Modifications for Mass Spectrometry" version="0.0" address="http://www.unimod.org/" />
<description>
<admin>
<sampleName>GPM06600002310</sampleName>
<sampleDescription comment="Ho, Y., et al., Systematic identification of protein complexes in Saccharomyces cerevisiae by mass spectrometry. Nature. 2002 Jan 10;415(6868):180-3.">
<cvParam cvLabel="NEWT" accession="4932" name="Saccharomyces cerevisiae (Baker's yeast)" value="Saccharomyces cerevisiae" />
</sampleDescription>
</admin>
</description>
<spectrumList count="0" />
</mzData>
</Experiment>
EOD
doc = Document.new xml
doc.elements["Experiment/Title"].text
doc.elements["Experiment/Protocol/ProtocolName"].text
doc.elements["Experiment/mzData/description/admin/sampleName"].text