Results 1 to 13 of 13
  1. #1
    sashko9 is offline Member
    Join Date
    Sep 2014
    Posts
    5
    Rep Power
    0

    Default TXT to XML conversion

    Anyone know of any online free tools/shareware I could use to convert my TXT file to an XML format?
    I tried using Notepad++ to save it as an XML format, but this did not work.

    Best Regards

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: TXT to XML conversion

    ... what? XML is also a text format :/
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    sashko9 is offline Member
    Join Date
    Sep 2014
    Posts
    5
    Rep Power
    0

    Default Re: TXT to XML conversion

    I have a plaintext file but eclipse won't let me parse it is in XML format.
    I get an error saying it doesn't "Follow the proper form" or something like that.

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: TXT to XML conversion

    I'm sure Eclipse is not lying to you. So what does this text file look like?

    And just to be sure - you know what XML actually is right?

    http://en.wikipedia.org/wiki/XML
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: TXT to XML conversion

    Quote Originally Posted by sashko9 View Post
    I have a plaintext file but eclipse won't let me parse it is in XML format.
    I get an error saying it doesn't "Follow the proper form" or something like that.
    It sounds like you are unfamiliar with XML. It is a text file which consists of tags (similar to HTML) and values.

    XML Code:
    <name>
    sashko9
    </name>
    If these aren't present along with the appropriate header information, then it won't be properly parsed as an XML
    file.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    sashko9 is offline Member
    Join Date
    Sep 2014
    Posts
    5
    Rep Power
    0

    Default Re: TXT to XML conversion

    Yeah that's my issue. So would I correct this simply by adding something like:

    Java Code:
    <?xml version="1.0" encoding="UTF-8"?>
    at the top of my current plain-text file?

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: TXT to XML conversion

    Quote Originally Posted by sashko9 View Post
    Yeah that's my issue. So would I correct this simply by adding something like:

    Java Code:
    <?xml version="1.0" encoding="UTF-8"?>
    at the top of my current plain-text file?
    It looks correct (but I haven't done this in a while). And of course, the file must conform to the XML
    syntax.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: TXT to XML conversion

    Its quicker to try it out than to post here. That header is correct... IF the data is actually UTF-8 encoded.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: TXT to XML conversion

    An XML file has structure and nesting of nodes. How would a program know how to nest nodes?

    For example, if the text file had this:
    fruit
    apple
    orange
    automobile
    ford
    2door
    4door
    fiat
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    sashko9 is offline Member
    Join Date
    Sep 2014
    Posts
    5
    Rep Power
    0

    Default Re: TXT to XML conversion

    I know they are both text formats, but for my purposes I'd like to turn it into a .xml extension via Java.

    Basically my current txt file has this format:

    Java Code:
    <DOC>
    <DOCNO>3393</DOCNO>
    <TEXT>
          Biblical Traditions  
    </TEXT>
    </DOC>
    
    
    <DOC>
    <DOCNO>42027</DOCNO>
    <TEXT>
        Automobiles   
    </TEXT>
    </DOC>
    
    
    <DOC>
    <DOCNO>7456</DOCNO>
    <TEXT>
         Fruits and Vegetables
    </TEXT>
    </DOC>
    The code I'm using in JAVA to do this is below:

    Java Code:
    package convert;
    import java.io.BufferedReader;
    import java.io.FileReader;
    import javax.xml.*;
    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.ParserConfigurationException;
    import javax.xml.transform.OutputKeys;
    import javax.xml.transform.Transformer;
    import javax.xml.transform.TransformerConfigurationException;
    import javax.xml.transform.TransformerException;
    import javax.xml.transform.TransformerFactory;
    import javax.xml.transform.dom.DOMSource;
    import javax.xml.transform.stream.StreamResult;
    import org.w3c.dom.*;
    
    
    public class convertToXML {
    	BufferedReader in;
    	StreamResult out;
    	
    	Document xmldoc;
    	Element root;
    
    	public static void main(String[] args) {
    		new convertToXML().doit();
    	}
    
    	public void doit() {
    		try {
    			in = new BufferedReader(new FileReader("C:\textfile.txt"));
    			out = new StreamResult("C:\xmlFile.xml");
    			initXML();
    			String str;
    			while ((str = in.readLine()) != null) {
    				process(str);
    			}
    			in.close();
    			writeXML();
    		}
    		catch (Exception e) { e.printStackTrace(); }
    	}
    	
    	public void initXML() throws ParserConfigurationException {
    	//JAXP + DOM
    	DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    	DocumentBuilder builder = factory.newDocumentBuilder();
    	DOMImplementation impl = builder.getDOMImplementation();
    	
    	xmldoc = impl.createDocument(null,  "DOC", null); 
    	root = xmldoc.getDocumentElement();
    }
    
    public void process(String s) {
    	//Escape character 
    	String [] elements = s.split("\\<");
    	Element e0 = xmldoc.createElement("DOC");
    	
    	Element e1 = xmldoc.createElement("TEXT");
    	Node n1 = xmldoc.createTextNode(elements[0]);
    	e1.appendChild(n1);
    	
    	//Element e2 = xmldoc.createElement("TEXT");
    	//Node n2 = xmldoc.createTextNode(elements[1]);
    	//e2.appendChild(n2);
    	
    	e0.appendChild(e1);
    	//e0.appendChild(e2);
    	root.appendChild(e0);
    }
    
    public String writeXML() throws TransformerConfigurationException, TransformerException {
    	DOMSource domSource = new DOMSource(xmldoc);
    	TransformerFactory tf = TransformerFactory.newInstance();
    	Transformer transformer = tf.newTransformer();
    	
    	transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    	transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    	transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
    	transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    
    	transformer.transform(domSource, out);
    	
    	java.io.StringWriter sw = new java.io.StringWriter();
    	StreamResult sr = new StreamResult(sw);
    	transformer.transform(domSource,  sr);
    	return sw.toString();
    }
    }
    I'm successful in obtaining the contents in the <TEXT> tags. That is, the title. I have trouble retrieving the DOCNO. Any ideas? I was having ArrayOutofBounds errors earlier when I was trying to include another value. Hence it is commented out. I found this code online, so I'm trying to work with this layout. Any help is appreciated.

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: TXT to XML conversion

    The txt file looks very close to having XML syntax/format. Have you tried to manually change it to a valid XML file to see what it takes?
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    sashko9 is offline Member
    Join Date
    Sep 2014
    Posts
    5
    Rep Power
    0

    Default Re: TXT to XML conversion

    Quote Originally Posted by Norm View Post
    The txt file looks very close to having XML syntax/format. Have you tried to manually change it to a valid XML file to see what it takes?
    Yeah man, I used Notepad++ and tried saving it as .xml
    When attempting to use that file in Java I get an error saying how it "doesn't follow the root structure" or something along those lines.

  13. #13
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,001
    Rep Power
    33

    Default Re: TXT to XML conversion

    Find some small valid xml files and compare their contents with the txt file.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. XLS to PDF conversion
    By nitin2k2k in forum Advanced Java
    Replies: 18
    Last Post: 07-02-2013, 11:17 AM
  2. Replies: 2
    Last Post: 02-22-2013, 06:10 PM
  3. Help with conversion please
    By bbesase in forum New To Java
    Replies: 0
    Last Post: 03-16-2011, 05:57 PM
  4. conversion
    By Dayanand in forum New To Java
    Replies: 4
    Last Post: 03-02-2011, 03:59 PM
  5. Doc to Pdf conversion
    By praveen.kb in forum Advanced Java
    Replies: 2
    Last Post: 01-16-2009, 12:27 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
  •