微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – 从嵌入html的xml中提取xml

我试图让xml在这里呈现http://www.ncbi.nlm.nih.gov/sra/ERX086768?report=FullXml,但它有点棘手,因为他们不给它任何支持.目的是将xml发送到PHP以便使用xml.

有人能暗示一下吗?

解决方法:

通过HTML呈现的XML不是真正的XML也不是真的.

你正在寻找的东西叫做textContent in DOMDocument.那只会给你那个HMTL的文字.就像在浏览器中显示“as text”一样.

所以你需要做的就是将HTML文档加载到DOMDocument.因为它包含错误,所以使用了内部错误

$url = 'http://www.ncbi.nlm.nih.gov/sra/ERX086768?report=FullXml';

$doc = new DOMDocument();
libxml_use_internal_errors(TRUE);
$doc->loadHTMLFile($url);
libxml_use_internal_errors(FALSE);

下一部分暗示了有关被抓取页面的具体知识.在您的情况下,XML是所有div-tag的所述文本内容,其中class属性为“xml-tag”*后跟*后面带有id“ResultView”的标记.

可以使用xpath查询轻松获取这些标记,然后将其文本内容存储到数组中:

$xpath  = new DOMXPath($doc);
$nodes  = $xpath->query('//*[@id="ResultView"]/following-sibling::div[@class="xml-tag"]');
$buffer = array();
foreach ($nodes as $node) {
    $buffer[] = $node->textContent;
}

所以现在剩下的就是创建一个新的DOMDocument并将XML缓冲区加载到其中,做一些很好的格式化和输出

$new = new DOMDocument();
$new->preserveWhiteSpace = FALSE;
$new->formatOutput = TRUE;
$new->loadXML(implode('', $buffer));
$new->save('PHP://output');

这些大约20行代码产生以下输出

<?xml version="1.0"?>
<EXPERIMENT_PACKAGE>
  <EXPERIMENT alias="SC_EXP_7229_8#56" center_name="SC" accession="ERX086768">
    <IDENTIFIERS>
      <PRIMARY_ID>ERX086768</PRIMARY_ID>
      <SUBMITTER_ID namespace="SC">SC_EXP_7229_8#56</SUBMITTER_ID>
    </IDENTIFIERS>
    <TITLE/>
    <STUDY_REF accession="ERP000913" refname="Genome_diversity_in_Streptococcus_dysgalactiae_subspecies_equisimilis-sc-2011-09-22T08:43:17Z-1977" refcenter="SC">
      <IDENTIFIERS>
        <PRIMARY_ID>ERP000913</PRIMARY_ID>
        <SUBMITTER_ID namespace="SC">Genome_diversity_in_Streptococcus_dysgalactiae_subspecies_equisimilis-sc-2011-09-22T08:43:17Z-1977</SUBMITTER_ID>
      </IDENTIFIERS>
    </STUDY_REF>
    <DESIGN>
      <DESIGN_DESCRIPTION>Standard</DESIGN_DESCRIPTION>
      <SAMPLE_DESCRIPTOR accession="ERS074283" refname="MR223754-sc-2011-11-18T11:31:44Z-1306470" refcenter="SC">
        <IDENTIFIERS>
          <PRIMARY_ID>ERS074283</PRIMARY_ID>
          <SUBMITTER_ID namespace="SC">MR223754-sc-2011-11-18T11:31:44Z-1306470</SUBMITTER_ID>
        </IDENTIFIERS>
      </SAMPLE_DESCRIPTOR>
      <LIBRARY_DESCRIPTOR>
        <LIBRARY_NAME>4008297</LIBRARY_NAME>
        <LIBRARY_STRATEGY>WGS</LIBRARY_STRATEGY>
        <LIBRARY_SOURCE>GENOMIC</LIBRARY_SOURCE>
        <LIBRARY_SELECTION>RANDOM</LIBRARY_SELECTION>
        <LIBRARY_LAYOUT>
          <PAIRED NOMINAL_LENGTH="250"/>
        </LIBRARY_LAYOUT>
      </LIBRARY_DESCRIPTOR>
      <SPOT_DESCRIPTOR>
        <SPOT_DECODE_SPEC>
          <READ_SPEC>
            <READ_INDEX>0</READ_INDEX>
            <READ_CLASS>Application Read</READ_CLASS>
            <READ_TYPE>Forward</READ_TYPE>
            <BASE_COORD>1</BASE_COORD>
          </READ_SPEC>
          <READ_SPEC>
            <READ_INDEX>1</READ_INDEX>
            <READ_CLASS>Application Read</READ_CLASS>
            <READ_TYPE>Reverse</READ_TYPE>
            <RELATIVE_ORDER follows_read_index="0"/>
          </READ_SPEC>
        </SPOT_DECODE_SPEC>
      </SPOT_DESCRIPTOR>
    </DESIGN>
    <PLATFORM>
      <ILLUMINA>
        <INSTRUMENT_MODEL>Illumina HiSeq 2000</INSTRUMENT_MODEL>
      </ILLUMINA>
    </PLATFORM>
    <PROCESSING/>
  </EXPERIMENT>
  <SUBMISSION accession="ERA119046" center_name="SC" submission_date="2012-04-17T09:29:50Z" alias="ERP000913-sc-20120417-2" lab_name="">
    <IDENTIFIERS>
      <PRIMARY_ID>ERA119046</PRIMARY_ID>
      <SUBMITTER_ID namespace="SC">ERP000913-sc-20120417-2</SUBMITTER_ID>
    </IDENTIFIERS>
  </SUBMISSION>
  <STUDY alias="Genome_diversity_in_Streptococcus_dysgalactiae_subspecies_equisimilis-sc-2011-09-22T08:43:17Z-1977" center_name="SC" accession="ERP000913">
    <IDENTIFIERS>
      <PRIMARY_ID>ERP000913</PRIMARY_ID>
      <SUBMITTER_ID namespace="SC">Genome_diversity_in_Streptococcus_dysgalactiae_subspecies_equisimilis-sc-2011-09-22T08:43:17Z-1977</SUBMITTER_ID>
    </IDENTIFIERS>
    <DESCRIPTOR>
      <STUDY_TITLE>Genome_diversity_in_Streptococcus_dysgalactiae_subspecies_equisimilis</STUDY_TITLE>
      <STUDY_TYPE existing_study_type="Whole Genome Sequencing"/>
      <STUDY_ABSTRACT>http://www.sanger.ac.uk/resources/downloads/bacteria/</STUDY_ABSTRACT>
      <CENTER_PROJECT_NAME>Genome_diversity_in_Streptococcus_dysgalactiae_subspecies_equisimilis</CENTER_PROJECT_NAME>
      <STUDY_DESCRIPTION>http://www.sanger.ac.uk/resources/downloads/bacteria/
This data is part of a pre-publication release. For information on the proper use of pre-publication data shared by the Wellcome Trust Sanger Institute (including details of any publication moratoria), please see http://www.sanger.ac.uk/datasharing/</STUDY_DESCRIPTION>
    </DESCRIPTOR>
  </STUDY>
  <SAMPLE alias="MR223754-sc-2011-11-18T11:31:44Z-1306470" center_name="SC" accession="ERS074283">
    <IDENTIFIERS>
      <PRIMARY_ID>ERS074283</PRIMARY_ID>
      <SUBMITTER_ID namespace="SC">MR223754-sc-2011-11-18T11:31:44Z-1306470</SUBMITTER_ID>
    </IDENTIFIERS>
    <SAMPLE_NAME>
      <COMMON_NAME>Streptococcus dysgalactiae subspecies equisimilis</COMMON_NAME>
      <TAXON_ID>119602</TAXON_ID>
      <SCIENTIFIC_NAME>Streptococcus dysgalactiae subsp. equisimilis</SCIENTIFIC_NAME>
    </SAMPLE_NAME>
    <SAMPLE_LINKS>
      <SAMPLE_LINK>
        <ENTREZ_LINK>
          <DB>biosample</DB>
          <ID>859730</ID>
        </ENTREZ_LINK>
      </SAMPLE_LINK>
    </SAMPLE_LINKS>
    <SAMPLE_ATTRIBUTES>
      <SAMPLE_ATTRIBUTE>
        <TAG>Strain</TAG>
        <VALUE>MR223754</VALUE>
      </SAMPLE_ATTRIBUTE>
      <SAMPLE_ATTRIBUTE>
        <TAG>Sample Description</TAG>
        <VALUE/>
      </SAMPLE_ATTRIBUTE>
      <SAMPLE_ATTRIBUTE>
        <TAG>ArrayExpress-StrainorLine</TAG>
        <VALUE>MR223754</VALUE>
      </SAMPLE_ATTRIBUTE>
      <SAMPLE_ATTRIBUTE>
        <TAG>ArrayExpress-Sex</TAG>
        <VALUE>not applicable</VALUE>
      </SAMPLE_ATTRIBUTE>
      <SAMPLE_ATTRIBUTE>
        <TAG>ArrayExpress-Species</TAG>
        <VALUE>Streptococcus dysgalactiae subspecies equisimilis</VALUE>
      </SAMPLE_ATTRIBUTE>
    </SAMPLE_ATTRIBUTES>
  </SAMPLE>
  <RUN_SET>
    <RUN alias="SC_RUN_7229_8#56" center_name="SC" accession="ERR109334" total_spots="2708543" total_bases="406281450" size="334475592" load_done="true" published="2012-04-27 20:11:35" is_public="true" cluster_name="public" static_data_available="1">
      <IDENTIFIERS>
        <PRIMARY_ID>ERR109334</PRIMARY_ID>
        <SUBMITTER_ID namespace="SC">SC_RUN_7229_8#56</SUBMITTER_ID>
      </IDENTIFIERS>
      <EXPERIMENT_REF refname="SC_EXP_7229_8#56" refcenter="SC" accession="ERX086768">
        <IDENTIFIERS>
          <PRIMARY_ID>ERX086768</PRIMARY_ID>
          <SUBMITTER_ID namespace="SC">SC_EXP_7229_8#56</SUBMITTER_ID>
        </IDENTIFIERS>
      </EXPERIMENT_REF>
      <Pool>
        <Member member_name="" accession="ERS074283" sample_name="MR223754-sc-2011-11-18T11:31:44Z-1306470" spots="2708543" bases="406281450"/>
      </Pool>
    </RUN>
  </RUN_SET>
</EXPERIMENT_PACKAGE>

因此,不要重新发明轮子,只需了解现有工具.它有时比第一眼看上去更容易.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。