Java DOM 解析器 XML

标签 java xml

我需要从 <Item Name="CanonicalSmiles"> 中提取属性值来自以下 XML 文件(已显示部分)?

我试过getElementsByTagName("Item").item(12).getTextContent());但对于不同的<DocSum> s item(i) 不同(即不总是 12!)

我该怎么做?

  <?xml version="1.0"?>
    <!DOCTYPE eSummaryResult PUBLIC "-//NLM//DTD eSummaryResult, 29 October 2004//EN" "http://www.ncbi.nlm.nih.gov/entrez/query/DTD/eSummary_041029.dtd">
    <eSummaryResult>
    <DocSum>
        <Id>53359352</Id>
        <Item Name="CID" Type="Integer">53359352</Item>
        <Item Name="SourceNameList" Type="List"></Item>
        <Item Name="SourceIDList" Type="List"></Item>
        <Item Name="SourceCategoryList" Type="List">
            <Item Name="string" Type="String">Journal Publishers</Item>
        </Item>
        <Item Name="CreateDate" Type="Date">2011/09/19 00:00</Item>
        <Item Name="SynonymList" Type="List"></Item>
        <Item Name="MeSHHeadingList" Type="List"></Item>
        <Item Name="MeSHTermList" Type="List"></Item>
        <Item Name="PharmActionList" Type="List"></Item>
        <Item Name="CommentList" Type="List"></Item>
        <Item Name="IUPACName" Type="String">2-hydroxy-6-[2-(4-hydroxyphenyl)-2-oxoethyl]benzoic acid</Item>
        <Item Name="CanonicalSmiles" Type="String">C1=CC(=C(C(=C1)O)C(=O)O)CC(=O)C2=CC=C(C=C2)O</Item>
        <Item Name="RotatableBondCount" Type="Integer">4</Item>
        <Item Name="MolecularFormula" Type="String">C15H12O5</Item>
        <Item Name="MolecularWeight" Type="String">272.252780</Item>
        <Item Name="TotalFormalCharge" Type="Integer">0</Item>
        <Item Name="XLogP" Type="String"></Item>
        <Item Name="HydrogenBondDonorCount" Type="Integer">3</Item>
        <Item Name="HydrogenBondAcceptorCount" Type="Integer">5</Item>
        <Item Name="Complexity" Type="String">359.000000</Item>
        <Item Name="HeavyAtomCount" Type="Integer">20</Item>
        <Item Name="AtomChiralCount" Type="Integer">0</Item>
        <Item Name="AtomChiralDefCount" Type="Integer">0</Item>
        <Item Name="AtomChiralUndefCount" Type="Integer">0</Item>
        <Item Name="BondChiralCount" Type="Integer">0</Item>
        <Item Name="BondChiralDefCount" Type="Integer">0</Item>
        <Item Name="BondChiralUndefCount" Type="Integer">0</Item>
        <Item Name="IsotopeAtomCount" Type="Integer">0</Item>
        <Item Name="CovalentUnitCount" Type="Integer">1</Item>
        <Item Name="TautomerCount" Type="Integer">67</Item>
        <Item Name="SubstanceIDList" Type="List"></Item>
        <Item Name="TPSA" Type="String">94.8</Item>
        <Item Name="AssaySourceNameList" Type="List"></Item>
        <Item Name="MinAC" Type="String"></Item>
        <Item Name="MaxAC" Type="String"></Item>
        <Item Name="MinTC" Type="String"></Item>
        <Item Name="MaxTC" Type="String"></Item>
        <Item Name="ActiveAidCount" Type="Integer">0</Item>
        <Item Name="InactiveAidCount" Type="Integer">0</Item>
        <Item Name="TotalAidCount" Type="Integer">0</Item>
        <Item Name="InChIKey" Type="String">YIGHIFUVVSYMFG-UHFFFAOYSA-N</Item>
        <Item Name="InChI" Type="String">InChI=1S/C15H12O5/c16-11-6-4-9(5-7-11)13(18)8-10-2-1-3-12(17)14(10)15(19)20/h1-7,16-17H,8H2,(H,19,20)</Item>
    </DocSum>

    <DocSum>
        <Id>53346823</Id>
        <Item Name="CID" Type="Integer">53346823</Item>
        <Item Name="SourceNameList" Type="List"></Item>
        <Item Name="SourceIDList" Type="List"></Item>
        <Item Name="SourceCategoryList" Type="List">
            <Item Name="string" Type="String">Biological Properties</Item>
        </Item>
        <Item Name="CreateDate" Type="Date">2011/09/01 00:00</Item>
        <Item Name="SynonymList" Type="List">
            <Item Name="string" Type="String">HMS2478O14</Item>
        </Item>
        <Item Name="MeSHHeadingList" Type="List"></Item>
        <Item Name="MeSHTermList" Type="List"></Item>
        <Item Name="PharmActionList" Type="List"></Item>
        <Item Name="CommentList" Type="List">
            <Item Name="string" Type="String">Asinex Ltd.:BAS 02768155</Item>
        </Item>
        <Item Name="IUPACName" Type="String">ethyl 3-amino-3-(1,3-benzodioxol-5-yl)propanoate chloride</Item>
        <Item Name="CanonicalSmiles" Type="String">CCOC(=O)CC(C1=CC2=C(C=C1)OCO2)N.[Cl-]</Item>
        <Item Name="RotatableBondCount" Type="Integer">5</Item>
        <Item Name="MolecularFormula" Type="String">C12H15ClNO4-</Item>
        <Item Name="MolecularWeight" Type="String">272.704800</Item>
        <Item Name="TotalFormalCharge" Type="Integer">-1</Item>
        <Item Name="XLogP" Type="String"></Item>
        <Item Name="HydrogenBondDonorCount" Type="Integer">1</Item>
        <Item Name="HydrogenBondAcceptorCount" Type="Integer">6</Item>
        <Item Name="Complexity" Type="String">271.000000</Item>
        <Item Name="HeavyAtomCount" Type="Integer">18</Item>
        <Item Name="AtomChiralCount" Type="Integer">1</Item>
        <Item Name="AtomChiralDefCount" Type="Integer">0</Item>
        <Item Name="AtomChiralUndefCount" Type="Integer">1</Item>
        <Item Name="BondChiralCount" Type="Integer">0</Item>
        <Item Name="BondChiralDefCount" Type="Integer">0</Item>
        <Item Name="BondChiralUndefCount" Type="Integer">0</Item>
        <Item Name="IsotopeAtomCount" Type="Integer">0</Item>
        <Item Name="CovalentUnitCount" Type="Integer">2</Item>
        <Item Name="TautomerCount" Type="Integer">1</Item>
        <Item Name="SubstanceIDList" Type="List"></Item>
        <Item Name="TPSA" Type="String">70.8</Item>
        <Item Name="AssaySourceNameList" Type="List"></Item>
        <Item Name="MinAC" Type="String"></Item>
        <Item Name="MaxAC" Type="String"></Item>
        <Item Name="MinTC" Type="String"></Item>
        <Item Name="MaxTC" Type="String"></Item>
        <Item Name="ActiveAidCount" Type="Integer">0</Item>
        <Item Name="InactiveAidCount" Type="Integer">0</Item>
        <Item Name="TotalAidCount" Type="Integer">0</Item>
        <Item Name="InChIKey" Type="String">NKQHQIJWIYNEIX-UHFFFAOYSA-M</Item>
        <Item Name="InChI" Type="String">InChI=1S/C12H15NO4.ClH/c1-2-15-12(14)6-9(13)8-3-4-10-11(5-8)17-7-16-10;/h3-5,9H,2,6-7,13H2,1H3;1H/p-1</Item>
    </DocSum>

最佳答案

对于您正在做的事情,XPath 可能比 DOM 更容易。请参阅this Java XPath tutorial .

关于Java DOM 解析器 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7507073/

相关文章:

java - 文件没有值

java - 如何确定图表面板上显示的数据量?

java - 如何注册 org.springframework.integration.monitor.IntegrationMBeanExporter

c++ - 使用带有 TinyXPath 和 TinyXML 的 XPath 获取属性

java - 如何从java中的编码中查找语言环境

android - 如何通过 Android 将 png 文件转换为 xml 资源?

java - 从 guiceModule 访问 dropwizard Bundle 实例

java - FreeMarker 模板中的 if-else

java - 当我通过我的设备(USB 调试)运行它时,我的 UI 会失真,但在我的 AVD 上运行良好

c# - 如何在C#中合并两个XmlDocuments