Visualizzazione dei risultati da 1 a 8 su 8
  1. #1

    Problema parsing file XML

    Ho dato un' occhiata ad un esempio di parsing di XML, č questo:

    codice:
    import java.io.File;
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    
    public class ReadXMLFile {
    
     public static void main(String argv[]) {
    
      try {
      File file = new File("MyXMLFile.xml");
      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
      DocumentBuilder db = dbf.newDocumentBuilder();
      Document doc = db.parse(file);
      doc.getDocumentElement().normalize();
      System.out.println("Root element " + doc.getDocumentElement().getNodeName());
      NodeList nodeLst = doc.getElementsByTagName("employee");
      System.out.println("Information of all employees");
    
      for (int s = 0; s < nodeLst.getLength(); s++) {
    
        Node fstNode = nodeLst.item(s);
        
        if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
      
               Element fstElmnt = (Element) fstNode;
          NodeList fstNmElmntLst = fstElmnt.getElementsByTagName("firstname");
    	    
          Element fstNmElmnt = (Element) fstNmElmntLst.item(0);
          NodeList fstNm = fstNmElmnt.getChildNodes();
          System.out.println("First Name : "  + ((Node) fstNm.item(0)).getNodeValue());
          NodeList lstNmElmntLst = fstElmnt.getElementsByTagName("lastname");
          Element lstNmElmnt = (Element) lstNmElmntLst.item(0);
          NodeList lstNm = lstNmElmnt.getChildNodes();
          System.out.println("Last Name : " + ((Node) lstNm.item(0)).getNodeValue());
        }
    
      }
      } catch (Exception e) {
        e.printStackTrace();
      }
     }
    }

    Il codice mi č abbastanza chiaro, tranne il perchč dell' istruzione:

    codice:
     NodeList fstNm = fstNmElmnt.getChildNodes();
          System.out.println("First Name : "  + ((Node) fstNm.item(0)).getNodeValue());
    Ho sperimentato che non funziona, ma perchč non č possibile fare:

    codice:
     Node fstNmElmnt =  fstNmElmntLst.item(0);
    che tanto sarą un nodo, quello che mi interessa, e poi nella System stampo il suo valore?


    Ora...in realtą il mio problema č un altro....se io ho un XML in questa forma:

    codice:
    <targets>
          <target position="1" partner="844">
            <actions>
              <action>antagonist</action>
            </actions>
            <references># Hosokawa N, Yamamoto S, Uehara Y, Hori M, Tsuchiya KS: Effect of thiazinotrienomycin B, an ansamycin antibiotic, on the function of epidermal growth factor receptor in human stomach tumor cells. J Antibiot (Tokyo). 1999 May;52(5):485-90. "Pubmed":http://www.ncbi.nlm.nih.gov/pubmed/10480573&#xD;
    # Wakita H, Takigawa M: Activation of epidermal growth factor receptor promotes late terminal differentiation of cell-matrix interaction-disrupted keratinocytes. J Biol Chem. 1999 Dec 24;274(52):37285-91. "Pubmed":http://www.ncbi.nlm.nih.gov/pubmed/10601294&#xD;
    # Suwa T, Ueda M, Jinno H, Ozawa S, Kitagawa Y, Ando N, Kitajima M: Epidermal growth factor receptor-dependent cytotoxic effect of anti-EGFR antibody-ribonuclease conjugate on human cancer cells. Anticancer Res. 1999 Sep-Oct;19(5B):4161-5. "Pubmed":http://www.ncbi.nlm.nih.gov/pubmed/10628369&#xD;
    # Burke P, Schooler K, Wiley HS: Regulation of epidermal growth factor receptor signaling by endocytosis and intracellular trafficking. Mol Biol Cell. 2001 Jun;12(6):1897-910. "Pubmed":http://www.ncbi.nlm.nih.gov/pubmed/11408594&#xD;
    # Viloria-Petit A, Crombet T, Jothy S, Hicklin D, Bohlen P, Schlaeppi JM, Rak J, Kerbel RS: Acquired resistance to the antitumor effect of epidermal growth factor receptor-blocking antibodies in vivo: a role for altered tumor angiogenesis. Cancer Res. 2001 Jul 1;61(13):5090-101. "Pubmed":http://www.ncbi.nlm.nih.gov/pubmed/11431346&#xD;
    # Chen X, Ji ZL, Chen YZ: TTD: Therapeutic Target Database. Nucleic Acids Res. 2002 Jan 1;30(1):412-5. "Pubmed":http://www.ncbi.nlm.nih.gov/pubmed/11752352</references>
            <known-action>yes</known-action>
          </target>
          <target position="2" partner="1102">
            <actions/>
            <references># Snyder LC, Astsaturov I, Weiner LM: Overview of monoclonal antibodies and small molecules targeting the epidermal growth factor receptor pathway in colorectal cancer. Clin Colorectal Cancer. 2005 Nov;5 Suppl 2:S71-80. "Pubmed":http://www.ncbi.nlm.nih.gov/pubmed/16336752</references>
            <known-action>unknown</known-action>
          </target>
          <target position="3" partner="3814">
            <actions/>
            <references># Overington JP, Al-Lazikani B, Hopkins AL: How many drug targets are there? Nat Rev Drug Discov. 2006 Dec;5(12):993-6. "Pubmed":http://www.ncbi.nlm.nih.gov/pubmed/17139284&#xD;
    # Imming P, Sinning C, Meyer A: Drugs, their targets and the nature and number of drug targets. Nat Rev Drug Discov. 2006 Oct;5(10):821-34. "Pubmed":http://www.ncbi.nlm.nih.gov/pubmed/17016423</references>
            <known-action>unknown</known-action>
          </target>
          <target position="4" partner="3815">
            <actions/>
            <references># Overington JP, Al-Lazikani B, Hopkins AL: How many drug targets are there? Nat Rev Drug Discov. 2006 Dec;5(12):993-6. "Pubmed":http://www.ncbi.nlm.nih.gov/pubmed/17139284&#xD;
    # Imming P, Sinning C, Meyer A: Drugs, their targets and the nature and number of drug targets. Nat Rev Drug Discov. 2006 Oct;5(10):821-34. "Pubmed":http://www.ncbi.nlm.nih.gov/pubmed/17016423</references>
            <known-action>unknown</known-action>
          </target>
          <target position="5" partner="3816">
            <actions/>
            <references># Overington JP, Al-Lazikani B, Hopkins AL: How many drug targets are there? Nat Rev Drug Discov. 2006 Dec;5(12):993-6. "Pubmed":http://www.ncbi.nlm.nih.gov/pubmed/17139284&#xD;
    # Imming P, Sinning C, Meyer A: Drugs, their targets and the nature and number of drug targets. Nat Rev Drug Discov. 2006 Oct;5(10):821-34. "Pubmed":http://www.ncbi.nlm.nih.gov/pubmed/17016423</references>
            <known-action>unknown</known-action>
          </target>
          <target position="6" partner="3817">
            <actions/>
            <references># Overington JP, Al-Lazikani B, Hopkins AL: How many drug targets are there? Nat Rev Drug Discov. 2006 Dec;5(12):993-6. "Pubmed":http://www.ncbi.nlm.nih.gov/pubmed/17139284&#xD;
    # Imming P, Sinning C, Meyer A: Drugs, their targets and the nature and number of drug targets. Nat Rev Drug Discov. 2006 Oct;5(10):821-34. "Pubmed":http://www.ncbi.nlm.nih.gov/pubmed/17016423</references>
            <known-action>unknown</known-action>
          </target>
          <target position="7" partner="3818">
            <actions/>
            <references># Overington JP, Al-Lazikani B, Hopkins AL: How many drug targets are there? Nat Rev Drug Discov. 2006 Dec;5(12):993-6. "Pubmed":http://www.ncbi.nlm.nih.gov/pubmed/17139284&#xD;
    # Imming P, Sinning C, Meyer A: Drugs, their targets and the nature and number of drug targets. Nat Rev Drug Discov. 2006 Oct;5(10):821-34. "Pubmed":http://www.ncbi.nlm.nih.gov/pubmed/17016423&#xD;
    # Zhang W, Gordon M, Schultheis AM, Yang DY, Nagashima F, Azuma M, Chang HM, Borucka E, Lurje G, Sherrod AE, Iqbal S, Groshen S, Lenz HJ: FCGR2A and FCGR3A polymorphisms associated with clinical outcome of epidermal growth factor receptor expressing metastatic colorectal cancer patients treated with single-agent cetuximab. J Clin Oncol. 2007 Aug 20;25(24):3712-8. "Pubmed":http://www.ncbi.nlm.nih.gov/pubmed/17704420</references>
            <known-action>unknown</known-action>
          </target>
          <target position="8" partner="2782">
            <actions/>
            <references># Overington JP, Al-Lazikani B, Hopkins AL: How many drug targets are there? Nat Rev Drug Discov. 2006 Dec;5(12):993-6. "Pubmed":http://www.ncbi.nlm.nih.gov/pubmed/17139284&#xD;
    # Imming P, Sinning C, Meyer A: Drugs, their targets and the nature and number of drug targets. Nat Rev Drug Discov. 2006 Oct;5(10):821-34. "Pubmed":http://www.ncbi.nlm.nih.gov/pubmed/17016423</references>
            <known-action>unknown</known-action>
          </target>
          <target position="9" partner="784">
            <actions/>
            <references># Negri DR, Tosi E, Valota O, Ferrini S, Cambiaggi A, Sforzini S, Silvani A, Ruffini PA, Colnaghi MI, Canevari S: In vitro and in vivo stability and anti-tumour efficacy of an anti-EGFR/anti-CD3 F(ab')2 bispecific monoclonal antibody. Br J Cancer. 1995 Oct;72(4):928-33. "Pubmed":http://www.ncbi.nlm.nih.gov/pubmed/7547242</references>
            <known-action>unknown</known-action>
          </target>
          <target position="10" partner="3819">
            <actions/>
            <references># Overington JP, Al-Lazikani B, Hopkins AL: How many drug targets are there? Nat Rev Drug Discov. 2006 Dec;5(12):993-6. "Pubmed":http://www.ncbi.nlm.nih.gov/pubmed/17139284&#xD;
    # Imming P, Sinning C, Meyer A: Drugs, their targets and the nature and number of drug targets. Nat Rev Drug Discov. 2006 Oct;5(10):821-34. "Pubmed":http://www.ncbi.nlm.nih.gov/pubmed/17016423&#xD;
    # Zhang W, Gordon M, Schultheis AM, Yang DY, Nagashima F, Azuma M, Chang HM, Borucka E, Lurje G, Sherrod AE, Iqbal S, Groshen S, Lenz HJ: FCGR2A and FCGR3A polymorphisms associated with clinical outcome of epidermal growth factor receptor expressing metastatic colorectal cancer patients treated with single-agent cetuximab. J Clin Oncol. 2007 Aug 20;25(24):3712-8. "Pubmed":http://www.ncbi.nlm.nih.gov/pubmed/17704420</references>
            <known-action>unknown</known-action>
          </target>
          <target position="11" partner="3820">
            <actions/>
            <references># Overington JP, Al-Lazikani B, Hopkins AL: How many drug targets are there? Nat Rev Drug Discov. 2006 Dec;5(12):993-6. "Pubmed":http://www.ncbi.nlm.nih.gov/pubmed/17139284&#xD;
    # Imming P, Sinning C, Meyer A: Drugs, their targets and the nature and number of drug targets. Nat Rev Drug Discov. 2006 Oct;5(10):821-34. "Pubmed":http://www.ncbi.nlm.nih.gov/pubmed/17016423</references>
            <known-action>unknown</known-action>
          </target>
          <target position="12" partner="3821">
            <actions/>
            <references># Overington JP, Al-Lazikani B, Hopkins AL: How many drug targets are there? Nat Rev Drug Discov. 2006 Dec;5(12):993-6. "Pubmed":http://www.ncbi.nlm.nih.gov/pubmed/17139284&#xD;
    # Imming P, Sinning C, Meyer A: Drugs, their targets and the nature and number of drug targets. Nat Rev Drug Discov. 2006 Oct;5(10):821-34. "Pubmed":http://www.ncbi.nlm.nih.gov/pubmed/17016423</references>
            <known-action>unknown</known-action>
          </target>
        </targets>
    In cui io devo prendere gli elementi del tipo:

    codice:
    <target position="1" partner="844">
    Ed in particolare solo il valore numerico di partner....sbaglio o l' esempio precedente non mi aiuta visto che mi permette di estrarre valori compresi tra tag aperti e chiusi?

  2. #2
    Utente di HTML.it L'avatar di Alex'87
    Registrato dal
    Aug 2001
    residenza
    Verona
    Messaggi
    5,802

    Re: Problema parsing file XML

    Originariamente inviato da Darčios89
    In cui io devo prendere gli elementi del tipo:

    codice:
    <target position="1" partner="844">
    Ed in particolare solo il valore numerico di partner....sbaglio o l' esempio precedente non mi aiuta visto che mi permette di estrarre valori compresi tra tag aperti e chiusi?
    partner č un attributo e la classe Element che stai usando ha un simpatico metodo che si chiama getAttribute(String), puoi usare quello per ottenere quello che ti serve...
    SpringSource Certified Spring Professional | Pivotal Certified Enterprise Integration Specialist
    Di questo libro e degli altri (blog personale di recensioni libri) | ​NO M.P. TECNICI

  3. #3
    Ma esattamente in che modo?

    Cioč dovrei fare:

    codice:
      NodeList nodeLst = doc.getElementsByTagName("<targets>");
    Per prendere l' intero blocco....e poi per quel valore:

    codice:
    Element fstElmnt = (Element) fstNode;
                  fstElmnt.getAttribute("partner");
    ?

  4. #4
    Utente di HTML.it L'avatar di Alex'87
    Registrato dal
    Aug 2001
    residenza
    Verona
    Messaggi
    5,802
    Originariamente inviato da Darčios89
    Ma esattamente in che modo?
    Per questo c'č la documentazione Queste classi non le conosco, ho googlato al volo la javadoc perché mi aspettavo un metodo per estrarre gli attributi...

    Originariamente inviato da Darčios89
    Cioč dovrei fare:

    codice:
      NodeList nodeLst = doc.getElementsByTagName("<targets>");
    Per prendere l' intero blocco....e poi per quel valore:

    codice:
    Element fstElmnt = (Element) fstNode;
                  fstElmnt.getAttribute("partner");
    ?
    Piano... Il tuo nodo radice č targets? (doc.getDocumentElement().getNodeName() ritorna "targets"?)

    Se sģ, mi aspetto che

    NodeList nodeLst = doc.getElementsByTagName("target");

    ti da un oggetto che incapsula tutti gli elementi col nome "target".
    NodeList quindi ha due metodi: getLength() e item(index): fai un ciclo da 0 a getLength(), ti fa dare l'i-esimo elemento, lo casti a Element e su quello chiami getAttribute("partner"). Ad occhio dovrebbe funzionare..
    SpringSource Certified Spring Professional | Pivotal Certified Enterprise Integration Specialist
    Di questo libro e degli altri (blog personale di recensioni libri) | ​NO M.P. TECNICI

  5. #5
    Io questi metodi non č che li capisco bene.....prima di parlare dei target sto facendo una cosa ancora pił basilare....prendere delle stringhe contenute in un tag <drug>

    codice:
    <drug-interactions>
          <drug-interaction>
            <drug>DB01381</drug>
            <name>Ginkgo biloba</name>
            <description>Additive anticoagulant/antiplatelet effects may increase bleed risk. Concomitant therapy should be avoided.</description>
          </drug-interaction>
          <drug-interaction>
            <drug>DB00374</drug>
            <name>Treprostinil</name>
            <description>The prostacyclin analogue, Treprostinil, increases the risk of bleeding when combined with the anticoagulant, Lepirudin. Monitor for increased bleeding during concomitant thearpy. </description>
          </drug-interaction>
        </drug-interactions>
        <protein-sequences>
          <protein-sequence>
            <header>DB00001 sequence</header>
            <chain>LVYTDCTESGQNLCLCEGSNVCGQGNKCILGSDGEKNQCVTGEGTPKPQSHNDGDFEEIPEEYLQ</chain>
          </protein-sequence>
        </protein-sequences>
        <experimental-properties>
          <property>
            <kind>logP/hydrophobicity</kind>
            <value>-0.777</value>
            <source></source>
          </property>
          <property>
            <kind>Molecular Weight</kind>
            <value>6963.4250</value>
            <source></source>
          </property>
          <property>
            <kind>Molecular Formula</kind>
            <value>C287H440N80O110S6</value>
            <source></source>
          </property>
        </experimental-properties>
        <external-identifiers>
          <external-identifier>
            <resource>Drugs Product Database (DPD)</resource>
            <identifier>2240996</identifier>
          </external-identifier>
          <external-identifier>
            <resource>National Drug Code Directory</resource>
            <identifier>50419-150-57</identifier>
          </external-identifier>
          <external-identifier>
            <resource>PharmGKB</resource>
            <identifier>PA450195</identifier>
          </external-identifier>
          <external-identifier>
            <resource>UniProtKB</resource>
            <identifier>P01050</identifier>
          </external-identifier>
        </external-identifiers>
        <external-links>
          <external-link>
            <resource>Wikipedia</resource>
            <url>http://en.wikipedia.org/wiki/Lepirudin</url>
          </external-link>
          <external-link>
            <resource>RxList</resource>
            <url>http://www.rxlist.com/cgi/generic/lepirudin.htm</url>
          </external-link>
          <external-link>
            <resource>Drugs.com</resource>
            <url>http://www.drugs.com/cdi/lepirudin.html</url>
          </external-link>
        </external-links>
        <targets>
          <target partner="54">
            <actions>
              <action>inhibitor</action>
            </actions>
            <references># Turpie AG: Anticoagulants in acute coronary syndromes. Am J Cardiol. 1999 Sep 2;84(5A):2M-6M. "Pubmed":http://www.ncbi.nlm.nih.gov/pubmed/10505536&#xD;
    # Warkentin TE: Venous thromboe

    E ho scritto un codice simile a quello di esempio:

    codice:
    import java.io.*;
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import org.w3c.dom.Document;
    import org.w3c.dom.Element;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    
    
    
    public class Prova{
    	
    	public static void main(String [] args)
    	{
    		
    		try{
    			File file = new File("drugbank.xml");
    			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    			DocumentBuilder db = dbf.newDocumentBuilder();
    			Document doc = db.parse(file);
    			doc.getDocumentElement().normalize();
    			NodeList nodeLst = doc.getElementsByTagName("drug-interactions");
    			
    			
    			for (int s = 0; s < nodeLst.getLength(); s++) {
    				
    			Node fstNode = nodeLst.item(s);
        
    			if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
    				
    			Element fstElmnt = (Element) fstNode;
    			NodeList fstNmElmntLst = fstElmnt.getElementsByTagName("drug");
    			Element fstNmElmnt = (Element) fstNmElmntLst.item(0);
    				
    			NodeList fstNm = fstNmElmnt.getChildNodes();
    		
    			System.out.println("Drug id: "  + ((Node)fstNm.item(0)).getNodeValue());
    				
    				}
    			}
    		
    		     }
    
    		
    		   catch(Exception e){e.printStackTrace();}
    		   
    	}
    }
    Ma ho un nullpointerException a
    codice:
    NodeList fstNm = fstNmElmnt.getChildNodes();
    Non riesco a capire questo parsing...

  6. #6
    Utente di HTML.it L'avatar di Alex'87
    Registrato dal
    Aug 2001
    residenza
    Verona
    Messaggi
    5,802
    Ti stai facendo dare gli elementi "drug-interactions" e da questi elementi ti fai fare i "drug". Ma tra drug-interactions e drug c'č drug-interaction, che non tratti da nessuna parte mi pare...

    Originariamente inviato da Darčios89
    Non riesco a capire questo parsing...
    Beh, ti sei letto la documentazione?
    SpringSource Certified Spring Professional | Pivotal Certified Enterprise Integration Specialist
    Di questo libro e degli altri (blog personale di recensioni libri) | ​NO M.P. TECNICI

  7. #7
    Si ho visto, la vedo a poco a poco perchč ci sono molte funzioni.....quindi uno dei problemi č quell' altro tag.....In teoria sostituendo solo con:

    codice:
    NodeList nodeLst = doc.getElementsByTagName("drug-interaction");
    Ottengo un output.....ma ci sono valori ripetuti.....stranamente...

    codice:
    Drug id: DB01381
    Drug id: DB00374
    Drug id: DB00072
    Drug id: DB00072
    Drug id: DB01609
    Drug id: DB00441
    Drug id: DB01381
    Drug id: DB06228
    Drug id: DB00374
    Drug id: DB01223
    Drug id: DB00651
    Drug id: DB01303
    Drug id: DB01265
    Drug id: DB00277
    Drug id: DB01381
    .
    .
    .

  8. #8
    Utente di HTML.it L'avatar di Alex'87
    Registrato dal
    Aug 2001
    residenza
    Verona
    Messaggi
    5,802
    Le duplicazioni ci sono anche nell'xml? Puoi linkarlo?

    Un suggerimento, dai dei nomi decenti e leggibili alle variabili.

    codice:
    Element fstElmnt = (Element) fstNode;
    NodeList fstNmElmntLst = fstElmnt.getElementsByTagName("drug");
    Element fstNmElmnt = (Element) fstNmElmntLst.item(0);
    fstElmnt, fstNmElmnt, fstNmElmntLst... Non si capisce un tubo di quello che dovrebbero contenere.
    SpringSource Certified Spring Professional | Pivotal Certified Enterprise Integration Specialist
    Di questo libro e degli altri (blog personale di recensioni libri) | ​NO M.P. TECNICI

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.