Results 1 to 4 of 4
  1. #1
    rajat16 is offline Member
    Join Date
    Sep 2010
    Posts
    2
    Rep Power
    0

    Post Will this code work

    Folks I am trying to parse XML file in which I have to read only the Field tags.

    please letme know whether this code will work.

    The Java Code

    Java Code:
    import java.io.File;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.ParserConfigurationException;
    
    import org.w3c.dom.Document;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    import org.xml.sax.SAXException;
    
    
    public class XMlTest {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		List<MTO> mtoList = new ArrayList<MTO>();
    		
    		try {
    			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    			DocumentBuilder db = dbf.newDocumentBuilder();
    			Document doc = db.parse(new File("C:\\MT_Savion_Delta_Request_Output.xml"));
    			
    			doc.normalize();
    			
    			NodeList props = doc.getElementsByTagName("item");
    			int props_length = props.getLength();
    			System.out.println(props_length);
    			for(int i=0; i<props_length;i++){
    				Node n = props.item(i);
    				mtoList.add(setValues(n));
    			}
    			
    		} catch (ParserConfigurationException e) {
    			e.printStackTrace();
    		} catch (SAXException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    
    	}
    	
    	
    	public static MTO setValues(Node n){
    		NodeList children = n.getChildNodes();
    		int length = children.getLength();
    		MTO mto = new MTO();
    		for(int i=0;i<length;i++){
    			Node c = children.item(i);
    			if(c.getTextContent()!=null){
    				if(c.getNodeName().equals("Field1")){
    					mto.setField1(c.getNodeValue());
    				} else 	if(c.getNodeName().equals("Field2")){
    					mto.setField2(c.getNodeValue());
    				} else if(c.getNodeName().equals("Field3")){
    					mto.setField3(c.getNodeValue());
    				} else if(c.getNodeName().equals("Field4")){
    					mto.setField4(c.getNodeValue());
    				} else if(c.getNodeName().equals("Field5")){
    					mto.setField5(c.getNodeValue());
    				} else if(c.getNodeName().equals("Field6")){
    					mto.setField6(c.getNodeValue());
    				} else if(c.getNodeName().equals("Field7")){
    					mto.setField7(c.getNodeValue());
    				} else if(c.getNodeName().equals("Field8")){
    					mto.setField8(c.getNodeValue());
    				} else if(c.getNodeName().equals("Field9")){
    					mto.setField9(c.getNodeValue());
    				} else if(c.getNodeName().equals("Field10")){
    					mto.setField10(c.getNodeValue());
    				} else if(c.getNodeName().equals("Field11")){
    					mto.setField11(c.getNodeValue());
    				} else if(c.getNodeName().equals("Field12")){
    					mto.setField12(c.getNodeValue());
    				} else if(c.getNodeName().equals("Field13")){
    					mto.setField13(c.getNodeValue());
    				} else if(c.getNodeName().equals("Field14")){
    					mto.setField14(c.getNodeValue());
    				}
    
    			}			
    		}
    		return mto;
    	}
    
    }
    The Pojo Code


    Java Code:
    public class MTO {
    	
    	private String Field1;
    	private String Field2;
    	private String Field3;
    	private String Field4;
    	private String Field5;
    	private String Field6;
    	private String Field7;
    	private String Field8;
    	private String Field9;
    	private String Field10;
    	private String Field11;
    	private String Field12;
    	private String Field13;
    	private String Field14;
    
    	
    	public String getField1() {
    		return Field1;
    	}
    	public void setField1(String Field1) {
    		this.Field1 = Field1;
    	}
    	public String getField2() {
    		return Field2;
    	}
    	public void setField2(String Field2) {
    		this.Field2 = Field2;
    	}
    	public String getField3() {
    		return Field3;
    	}
    	public void setField3(String Field3) {
    		this.Field3 = Field3;
    	}
    	public String getField4() {
    		return Field4;
    	}
    	public void setField4(String Field4) {
    		this.Field4 = Field4;
    	}
    	public String getField5() {
    		return Field5;
    	}
    	public void setField5(String Field5) {
    		this.Field5 = Field5;
    	}
    	public String getField6() {
    		return Field6;
    	}
    	public void setField6(String Field6) {
    		this.Field6 = Field6;
    	}
    	public String Field7() {
    		return Field7;
    	}
    	public void setField7(String Field7) {
    		this.Field7 = Field7;
    	}
    	
    	public String Field8() {
    		return Field8;
    	}
    	public void setField8(String Field8) {
    		this.Field8 = Field8;
    	}
    
    	public String Field9() {
    		return Field9;
    	}
    	public void setField9(String Field9) {
    		this.Field9 = Field9;
    	}
    
    	public String Field10() {
    		return Field10;
    	}
    	public void setField10(String Field10) {
    		this.Field10 = Field10;
    	}
    	
    	public String Field11() {
    		return Field11;
    	}
    	public void setField11(String Field11) {
    		this.Field11 = Field11;
    	}
    
    	public String Field12() {
    		return Field12;
    	}
    	public void setField12(String Field12) {
    		this.Field12 = Field12;
    	}
    
    	public String Field13() {
    		return Field13;
    	}
    	public void setField13(String Field13) {
    		this.Field13 = Field13;
    	}
    
    	public String Field14() {
    		return Field14;
    	}
    	public void setField14(String Field14) {
    		this.Field14 = Field14;
    	}
    
    }
    XML file


    Java Code:
    import java.io.File;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.ParserConfigurationException;
    
    import org.w3c.dom.Document;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    import org.xml.sax.SAXException;
    
    
    public class XMlTest {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		List<MTO> mtoList = new ArrayList<MTO>();
    		
    		try {
    			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    			DocumentBuilder db = dbf.newDocumentBuilder();
    			Document doc = db.parse(new File("C:\\MT_Savion_Delta_Request_Output.xml"));
    			
    			doc.normalize();
    			
    			NodeList props = doc.getElementsByTagName("item");
    			int props_length = props.getLength();
    			System.out.println(props_length);
    			for(int i=0; i<props_length;i++){
    				Node n = props.item(i);
    				mtoList.add(setValues(n));
    			}
    			
    		} catch (ParserConfigurationException e) {
    			e.printStackTrace();
    		} catch (SAXException e) {
    			e.printStackTrace();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    
    	}
    	
    	
    	public static MTO setValues(Node n){
    		NodeList children = n.getChildNodes();
    		int length = children.getLength();
    		MTO mto = new MTO();
    		for(int i=0;i<length;i++){
    			Node c = children.item(i);
    			if(c.getTextContent()!=null){
    				if(c.getNodeName().equals("Field1")){
    					mto.setField1(c.getNodeValue());
    				} else 	if(c.getNodeName().equals("Field2")){
    					mto.setField2(c.getNodeValue());
    				} else if(c.getNodeName().equals("Field3")){
    					mto.setField3(c.getNodeValue());
    				} else if(c.getNodeName().equals("Field4")){
    					mto.setField4(c.getNodeValue());
    				} else if(c.getNodeName().equals("Field5")){
    					mto.setField5(c.getNodeValue());
    				} else if(c.getNodeName().equals("Field6")){
    					mto.setField6(c.getNodeValue());
    				} else if(c.getNodeName().equals("Field7")){
    					mto.setField7(c.getNodeValue());
    				} else if(c.getNodeName().equals("Field8")){
    					mto.setField8(c.getNodeValue());
    				} else if(c.getNodeName().equals("Field9")){
    					mto.setField9(c.getNodeValue());
    				} else if(c.getNodeName().equals("Field10")){
    					mto.setField10(c.getNodeValue());
    				} else if(c.getNodeName().equals("Field11")){
    					mto.setField11(c.getNodeValue());
    				} else if(c.getNodeName().equals("Field12")){
    					mto.setField12(c.getNodeValue());
    				} else if(c.getNodeName().equals("Field13")){
    					mto.setField13(c.getNodeValue());
    				} else if(c.getNodeName().equals("Field14")){
    					mto.setField14(c.getNodeValue());
    				}
    
    			}			
    		}
    		return mto;
    	}
    
    }
    Last edited by Eranga; 09-24-2010 at 11:53 AM. Reason: code tags added

  2. #2
    rajat16 is offline Member
    Join Date
    Sep 2010
    Posts
    2
    Rep Power
    0

    Default xml file

    sorry the XML file is attached
    Attached Files Attached Files

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Why don't you run yourself and check whether your code is working or not. I don't think that anyone wants to run your code. If you are stuck on anything then please ask your question more clearly.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    And also please use code tags when you are posting again. Unformated codes are really hard to read. If you don't know how to do it, check on my forum signature. You can find a relevant URL.

Similar Threads

  1. I can't get my code to work?
    By ComicStix in forum New To Java
    Replies: 5
    Last Post: 09-19-2010, 03:15 PM
  2. This code doesn't work. Why? thanks.
    By seanzhang in forum New To Java
    Replies: 10
    Last Post: 07-09-2010, 05:53 AM
  3. Code does not work
    By scotts in forum New To Java
    Replies: 12
    Last Post: 06-19-2010, 09:37 AM
  4. Why this code don't work?
    By artemff in forum CLDC and MIDP
    Replies: 6
    Last Post: 04-16-2010, 02:57 AM
  5. How does the '*' work in the code?
    By DrMath in forum New To Java
    Replies: 3
    Last Post: 10-28-2009, 09:26 AM

Posting Permissions

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