Results 1 to 9 of 9

Thread: XML output file

  1. #1
    Lund01's Avatar
    Lund01 is offline Senior Member
    Join Date
    Sep 2010
    Location
    The Netherlands
    Posts
    149
    Rep Power
    0

    Question XML output file

    Hi everyone,

    At the moment I am making a Converter.
    My program can already read the data from the file I need, but we would like as an output a .xml file.

    This output-file needs to look like this.
    I would like some help with this, but it only needs is to look like this:

    Java Code:
     
    <spectrum=[COLOR="blue"]"Kilamanjaro12" scan="1"[/COLOR]>
    </spectrum>
    At the moment my output is this:

    Java Code:
    <data>
       <SpecFile>[COLOR="blue"]Kilamanjaro12[/COLOR]</SpecFile>
       <Scan>[COLOR="blue"]1[/COLOR]</Scan>
    </data>
    But how do I get the information out of the file (the bleu part) on that position behind/in that tag area?

    My java code:

    Java Code:
     
    import java.io.*;
    import java.util.*;
    import org.w3c.dom.*;
    import javax.xml.parsers.*;
    import javax.xml.transform.*;
    import javax.xml.transform.dom.*;
    import javax.xml.transform.stream.*;
    
    public class pepXMLConverter {
    	public static void main(String[] args) throws Exception {
    
    		// Make new Document
    		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    		DocumentBuilder docBuilder = dbf.newDocumentBuilder();
    		Document doc = docBuilder.newDocument();
    
    		// Create and add a root element.
    		Element elt = doc.createElement("root");
    		doc.appendChild(elt);
    
    		try {
    			// Create an instance for the file that needs to be loaded in the script.
    			File file1 = new File("example.msgfdb");
    			Scanner s = new Scanner(file1);
    
    			// Keeps track of the count.
    			int getal = 0;
    
    			// We read the lines from file1 one by one.
    			while (s.hasNextLine()) {
    				String line = s.nextLine();
    
    				// Skip the first line, and start with the second line.
    				if (getal >= 1) {
    
    					// Create Child0.
    					Element child0 = doc.createElement("data");
    					elt.appendChild(child0);
    
    					// Create Child1.
    					Element child1 = doc.createElement("SpecFile");
    					child0.appendChild(child1);
    					// Create Child2.
    					Element child2 = doc.createElement("Scan");
    					child0.appendChild(child2);
    
    					// Read each line and seperate the words.
    					// After a 'tab space' the word gets seperated.
    					String[] items = line.split("\t");
    					String infoChild1 = items[0];
    					String infoChild2 = items[1];
    
    					// Add text to child1.
    					Text text1 = doc.createTextNode(infoChild1);
    					child1.appendChild(text1);
    					// Add text to child2.
    					Text text2 = doc.createTextNode(infoChild2);
    					child2.appendChild(text2);
    				}
    
    				// ++ adds +1 every line.
    				getal++;
    			}
    
    			// Set up a transformer.
    			TransformerFactory transFac = TransformerFactory.newInstance();
    			Transformer trans = transFac.newTransformer();
    			trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    			trans.setOutputProperty(OutputKeys.INDENT, "yes");
    
    			StringWriter sw = new StringWriter();
    			// Save to file.
    			StreamResult result = new StreamResult(new File("Converter_Output.xml"));
    			DOMSource source = new DOMSource(doc);
    			trans.transform(source, result);
    
    		} catch (FileNotFoundException e) {
    			e.printStackTrace();
    		}
    	}
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    I don't do this sort of coding, but I looked at the API and see that since you want to create an attribute, so you might have better luck calling Element's setAttribute method rather than Document's createElement, but I'm not sure. Let me know if this helps.

    edit: something along these lines perhaps?

    Java Code:
                if (getal >= 1) {
    
                   // Create Child0.
                   Element child0 = doc.createElement("data");
                   elt.appendChild(child0);
    
                   // Create Child1.
                   Element child1 = doc.createElement("SpecFile");
                   child0.appendChild(child1);
    
                   String[] items = line.split("\t");
                   String infoChild1 = items[0];
                   String infoChild2 = items[1];
    
                   child1.setAttribute("spectrum", infoChild1);
                   child1.setAttribute("Scan", infoChild2);
                }
    Last edited by Fubarable; 06-21-2011 at 11:59 PM.

  3. #3
    lovelesh is offline Senior Member
    Join Date
    Nov 2010
    Location
    Delhi
    Posts
    135
    Blog Entries
    1
    Rep Power
    0

    Default

    Fubarable is correct. Thats the way to do it.

  4. #4
    Lund01's Avatar
    Lund01 is offline Senior Member
    Join Date
    Sep 2010
    Location
    The Netherlands
    Posts
    149
    Rep Power
    0

    Default Thanks :)

    Thanks for the help.
    It idd was the thing I was looking for.
    So then I now can makes the program complete.

  5. #5
    Lund01's Avatar
    Lund01 is offline Senior Member
    Join Date
    Sep 2010
    Location
    The Netherlands
    Posts
    149
    Rep Power
    0

    Default

    I have done this for the complete file.
    And it idd is what I was looking for.
    There is only one small thing I don't fully understand.
    Becose everything between the tags he puts it automaticly in order of the alphabet.

    What I would like:
    Java Code:
    <spectrum [COLOR="blue"]name="Kilamanjaro12"[/COLOR] [COLOR="magenta"]start_scan="1"[/COLOR] [COLOR="darkorange"]end_scan="2"[/COLOR]>
    </spectrum>
    What I now have:
    Java Code:
     
    <spectrum [COLOR="darkorange"]end_scan="2"[/COLOR] [COLOR="blue"]name="Kilamanjaro12"[/COLOR] [COLOR="magenta"]start_scan="1"[/COLOR] >
    </spectrum>

  6. #6
    lovelesh is offline Senior Member
    Join Date
    Nov 2010
    Location
    Delhi
    Posts
    135
    Blog Entries
    1
    Rep Power
    0

    Post

    Could you please paste the relevant code, so that we can see what you have done.

  7. #7
    Lund01's Avatar
    Lund01 is offline Senior Member
    Join Date
    Sep 2010
    Location
    The Netherlands
    Posts
    149
    Rep Power
    0

    Default

    This is my code now.
    The other part I haven't changed anything on what I have shown above in my first post.

    Java Code:
    				// Skip the first line, and start with the second line.
    				if (getal >= 1) {
    
    					// Create the main tags:
    					// Create Child0.
    					Element child0 = doc.createElement("spectrum");
    					elt.appendChild(child0);
    
    					// Read each line and seperate the words.
    					// After a 'tab space' the word gets seperated.
    					String[] items = line.split("\t");
    					String specFileInfo = items[0];
    					String scanInfo = items[1];
    					String actMethodInfo = items[2];
    					String precursorInfo = items[3];
    					String chargeInfo = items[4];
    					String peptideInfo = items[5];
    					String proteinInfo = items[6];
    					String deNovoScoreInfo = items[7];
    					String msgfScoreInfo = items[8];
    					String specProbInfo = items[9];
    
    					// Add Attributes to the child0 tag.
    					child0.setAttribute("assumed_charge", chargeInfo);
    					child0.setAttribute("precursor_neutral_mass", precursorInfo);
    					child0.setAttribute("end_scan", scanInfo);
    					child0.setAttribute("start_scan", scanInfo);
    				}
    And now everything that is inbetween the xml tags get automaticly sorted out in alphabet order.

  8. #8
    lovelesh is offline Senior Member
    Join Date
    Nov 2010
    Location
    Delhi
    Posts
    135
    Blog Entries
    1
    Rep Power
    0

    Default

    Ideally, the reader of output file should not be concerned with the order of attributes. I think AttributeNode can be used instead of setAttribute.

    See the example in this link : java - order the sequence of DOM attributes of an element - Stack Overflow

    This might help :-)

  9. #9
    Lund01's Avatar
    Lund01 is offline Senior Member
    Join Date
    Sep 2010
    Location
    The Netherlands
    Posts
    149
    Rep Power
    0

    Default

    So in that case it wouldn't mind?
    And I can leave it that way?

Similar Threads

  1. Replies: 0
    Last Post: 09-30-2010, 08:16 PM
  2. Replies: 8
    Last Post: 05-12-2010, 07:19 PM
  3. Need help formatting File Output
    By aaroncarpet in forum New To Java
    Replies: 2
    Last Post: 11-26-2009, 05:26 PM
  4. No output to file
    By shiva in forum Java Servlet
    Replies: 0
    Last Post: 04-24-2009, 02:06 PM
  5. Replies: 7
    Last Post: 11-19-2008, 02:09 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •