2011-09-21 6 views
2

次のXMLファイルから属性値を抽出する必要があります(一部表示)<Item Name="CanonicalSmiles">から属性値を抽出する必要がありますか?Java DOM Parser XML

私はgetElementsByTagName("Item").item(12).getTextContent());を試みたが、異なる<DocSum>の項目(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> 
+1

サンプルXMLを縮小し、正準形式に変換したい場合があります。あなたはそのように注意を向けるでしょう。 – RHT

+0

dom4j APIなどのxpath互換のDOMパーサーを使用します。 –

+1

@Usman:標準JavaはXPathを実行するので、なぜdom4jが必要ですか? –

答えて

1
XPathFactory xpf = XPathFactory.newInstance(); 
    XPath xp = xpf.newXPath(); 
    XPathExpression xe = xp.compile("//DocSum/Item[@Name='CanonicalSmiles']/text()"); 
    NodeList nodes = (NodeList)xe.evaluate(yourdom, XPathConstants.NODESET); 
+0

ありがとう、トリックでした。それから、(int i = 0; i lochi

3

XPathは、DOMよりも簡単です。 this Java XPath tutorialを参照してください。

+0

チュートリアルへのリンクありがとう – lochi

0

他の人が指摘しているように、XPathは標準的な方法です。あなたはjOOXのようなツールを使用している場合は、XPathを書くことはもっと簡単です:

String text = $(document).xpath("//DocSum/Item[@Name='CanonicalSmiles']").text(); 

jOOXで、あなたはしかし、XPathを使用する必要はありません。また、フィルタを使用して、例えば、直接jOOXのjQueryのようなAPIを使用することができます。

String text = $(document).find("Item") 
         .filter(attr("Name", "CanonicalSmiles")) 
         .text(); 

またはCSSスタイルのセレクターを使用して:

String text = $(document).find("Item[Name='CanonicalSmiles']").text(); 
+0

興味深いことに、私はAPIとほぼ同じように作業していました。 - https://github.com/jjnguy/Jinq2XML/ – jjnguy

+0

@jjnguy:その後、停止してjOOXに貢献してください! :-) jOOXはまだLINQスタイルのAPIが欠けているでしょう。私はあなたがすでに持っているものをチェックします。あなたのクラス名のように: 'Jode'、' Jocument' ;-) –

0

私は見ての通り、パーサの問題たびに読み取りを異なる順序のXML要素には依然として未回答のままです。

XMLには要素の順序はありません。要素がnumとして読み込まれるのを待つことはできません。 12今日はnumです。明日12日。あなたの要素に番号を付ける唯一の方法は、数字を明示的に与えることです。

<Item Name="TotalFormalCharge" Type="Integer">-1</Item> 

はなります:

<Item Name="TotalFormalCharge" Num=6 Type="Integer">-1</Item> 

そして、あなたは属性値によってそれを得ることができます。

+0

この質問は答えられました。上記の答えを見てください。とにかく、それは2年前にソフトウェアで解決され、実装されました。 – lochi

+0

1.あなたには複数の問題があります:「異なる項目(i)が異なっています(つまり、常に12ではありません)」は未回答のままです。私はそれに答えた。 2.ここに掲載された記事は、著者だけのものではなく、他の人々のためのものでもある。それ以外の場合は役に立つかもしれません。また、古い質問に役立つ人には、いくつかのバッジがあります。 – Gangnus