Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: read xml files and write to the txt file

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default read xml files and write to the txt file

    Hi,
    My background is mainframe and i'm new to java. We're moving from mainframe to the java world and i'm trying to achieve a task. I have a main folder and then bunch of sub folders and each sub folder has bunch of xml files, files size are varies some of them are 900kb. I need to read these xml files and send the output to the txt file (comma separated). I would really appreciate if someone can put me on the right track and advise me some possible options or even sample example would be great. please find below the sample xml file. I need to extract information's only where FORM name="F00001".

    <PACKET>
       <FORM name="F00001" sequence="0">
          <FIELD sequence="1">02</FIELD>
          <FIELD sequence="2">00000</FIELD>
          <FIELD sequence="3">CAB100</FIELD>
          <FIELD sequence="4">TEFAP001</FIELD>
          <FIELD sequence="5">EMPTY FIELD</FIELD>
          <FIELD sequence="6">009.999.989</FIELD>
          <FIELD sequence="7">03</FIELD>
          <FIELD sequence="8">P9999999999999</FIELD>
          <FIELD sequence="9">EMPTY FIELD</FIELD>
          <FIELD sequence="10">EMPTY FIELD</FIELD>
       </FORM>
       <FORM name="F99999" sequence="1"
          <FIELD sequence="1">TIM K.</FIELD>
          <FIELD sequence="2">PROFESSOR</FIELD>
          <FIELD sequence="3">DAVID P. PIT</FIELD>
          <FIELD sequence="4">ASST PROFESSOR</FIELD>
          <FIELD sequence="5">EMPTY FIELD</FIELD>
          <FIELD sequence="6">009.999.989</FIELD>
          <FIELD sequence="7">03</FIELD>
          <FIELD sequence="8">P9999999999999</FIELD>
          <FIELD sequence="9">EMPTY FIELD</FIELD>
          <FIELD sequence="10">EMPTY FIELD</FIELD>
       </FORM>
    </PACKET>
    <PACKET>
       <FORM name="F00001" sequence="0">
          <FIELD sequence="1">09</FIELD>
          <FIELD sequence="2">00999</FIELD>
          <FIELD sequence="3">CAB101</FIELD>
          <FIELD sequence="4">TEFAP9999</FIELD>
          <FIELD sequence="5">EMPTY FIELD</FIELD>
          <FIELD sequence="6">009.999.999</FIELD>
          <FIELD sequence="7">03</FIELD>
          <FIELD sequence="8">T9999999999999</FIELD>
          <FIELD sequence="9">EMPTY FIELD</FIELD>
          <FIELD sequence="10">EMPTY FIELD</FIELD>
       </FORM>
       <FORM name="F99998" sequence="1"
          <FIELD sequence="1">KIM T.</FIELD>
          <FIELD sequence="2">PROFESSOR</FIELD>
          <FIELD sequence="3">NATHAN B.</FIELD>
          <FIELD sequence="4">ASST PROFESSOR</FIELD>
          <FIELD sequence="5">EMPTY FIELD</FIELD>
          <FIELD sequence="6">009.999.989</FIELD>
          <FIELD sequence="7">08</FIELD>
          <FIELD sequence="8">P9999999999988</FIELD>
          <FIELD sequence="9">EMPTY FIELD</FIELD>
          <FIELD sequence="10">EMPTY FIELD</FIELD>
       </FORM>
    </PACKET>

    Thank you so much

    best regards
    rm


  2. #2
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: read xml files and write to the txt file

    You can use either JAXP or JAXB API. Using JAXP you will parse xml documents and build text file on the fly. Using JAXB, you can convert xml data into java objects. Then you print objects using comma separated values to text files.

    If you want you can try Camel fraemwork. This framework is complex and will need time to be mastered. But once you master it your task reduces to few lines of code compared to several lines when JAXP/JAXB are used.

    Thanks,
    Syed.

  3. #3
    Junior Member
    Join Date
    Jul 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: read xml files and write to the txt file

    Thanks for the reply, appreciate it.

    I'm using xpath to parse the data. I need to read multiple input files from the folder i.e. c:/java/, can somoneplease help me.

    import java.io.IOException;
    import org.w3c.dom.*;
    import org.xml.sax.SAXException;
    import javax.xml.parsers.*;
    import javax.xml.xpath.*;
     
    public class XPathExample {
     
    	public static void main(String[] args) throws ParserConfigurationException,
    			SAXException, IOException, XPathExpressionException {
     
    		DocumentBuilderFactory domFactory = DocumentBuilderFactory
    				.newInstance();
    		domFactory.setNamespaceAware(true); 
    		DocumentBuilder builder = domFactory.newDocumentBuilder();
    		Document doc = builder.parse("c:/java/test.xml");
     
    		XPathFactory factory = XPathFactory.newInstance();
    		XPath xpath = factory.newXPath();
    		XPathExpression expr = xpath.compile("//FORM[@name='F00001']/*/text()");
     
    		Object result = expr.evaluate(doc, XPathConstants.NODESET);
    		NodeList nodes = (NodeList) result;
    		for (int i = 0; i < nodes.getLength(); i++) {
    			System.out.println(nodes.item(i).getNodeValue());
    		}
     
    	}
     
    }

    Thanks
    rm

  4. #4
    Junior Member
    Join Date
    Jul 2018
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: read xml files and write to the txt file

    Quote Originally Posted by syedbhai View Post
    You can use either JAXP or JAXB API. Using JAXP you will parse xml documents and build text file on the fly. Using JAXB, you can convert xml data into java objects. Then you print objects using comma separated values to text files.

    If you want you can try Camel fraemwork. This framework is complex and will need time to be mastered. But once you master it your task reduces to few lines of code compared to several lines when JAXP/JAXB are used.

    Thanks,
    Syed.

    Hi Syed,

    Can you please help me with Camel example how to convert XML file to FlatFile.

    Thanks
    Jasleen

Similar Threads

  1. how to read and execute the whole .txt file
    By epulcipun in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: March 21st, 2013, 03:18 PM
  2. Replies: 0
    Last Post: November 13th, 2012, 07:02 AM
  3. How can I read txt files in java program??
    By sakura_smile in forum What's Wrong With My Code?
    Replies: 5
    Last Post: June 9th, 2012, 06:18 PM
  4. convert excel to xml and read the input from xml file
    By rahulruns in forum Object Oriented Programming
    Replies: 5
    Last Post: April 3rd, 2012, 11:13 AM
  5. [SOLVED] Write an xml dom to a xml file
    By Kakashi in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: March 2nd, 2011, 03:30 PM