2011-09-21 7 views
2

다음 XML 파일에서 <Item Name="CanonicalSmiles">의 속성 값을 추출해야합니까 (일부만 표시)? 자바 DOM 파서 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 호환 돔 파서를 사용합니다. –

+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를 사용할 필요가 없습니다.

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

또는 사용하여 CSS 스타일의 선택기로 : 당신은 또한 예를 사용하여 필터에 대한 직접 jOOX의 jQuery를 같은 API를 사용할 수 내가 보는 것처럼

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

충분히 흥미롭게도 나는 너와 거의 똑같은 API를 연구 중이다. - https://github.com/jjuguy/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. 여기에 게시 된 글은 질문 작성자 용이 아닌 smb 용 다른 사람들에게도 해당됩니다. 그렇지 않으면 유용 할 수 있습니다. 유용하게도 낡은 질문에 다시 답할 수있는 사람에게 부여되는 배지도 있습니다. – Gangnus