1

I found and followed an example from Stackoverflow (http://stackoverflow.com/questions/2310139/how-to-read-xml-response-from-a-url-in-java) of how to read an XML file from a URL (as you can see in my code pasted below). My only trouble is that now that I got the program to read the XML, how do I get it to store it? For example, could I make it save the information to a XML file built into the project (this would be the best solution for me, if it's possible)? Such as, take for example, I have a blank XML file built into the project. The program runs, reads the XML code off of the URL, and stores it all into the pre-built blank XML file. Could I do this?

If I sound confusing or un-clear about anything, just ask me to clarify what I'm looking for.

And here is my code, if you'd like to look at what I have so far:

package xml.parsing.example;

import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
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.Document;
import org.xml.sax.SAXException;

public class XmlParser {
    public static void main (String[] args) throws IOException, ParserConfigurationException, SAXException, TransformerException {
        URL url = new URL("http://totheriver.com/learn/xml/code/employees.xml");
        URLConnection conn = url.openConnection();

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(conn.getInputStream());

        TransformerFactory tfactory = TransformerFactory.newInstance();
        Transformer xform = tfactory.newTransformer();

        // that’s the default xform; use a stylesheet to get a real one
        xform.transform(new DOMSource(doc), new StreamResult(System.out));
    }
}
Mxyk
  • 10,678
  • 16
  • 57
  • 76
  • You're already writing the transform output to `System.out`, why can't you just substitute a `StreamResult(File...)`? – Jim Garrison Aug 18 '11 at 17:48
  • I'm still a novice with this, so I don't know what that is/how I'd write the code for it. If you could show mean what you mean in an answer to this question, I'd greatly appreciate it! It sounds like I'm not missing too much. – Mxyk Aug 18 '11 at 17:51

2 Answers2

4

Very simply:

File myOutput = new File("c:\\myDirectory\\myOutput.xml");
xform.transform(new DOMSource(doc), new StreamResult(myOutput));
Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
  • Awesome! That will work for this application, but I should have mentioned that eventually I will be having an Android App be reading the XML from the URL. Is there any way I can do this, only have it save directly to the project's resource folder? If you're on an android phone, it obviously can't save a file to a computer directory :-P. – Mxyk Aug 18 '11 at 17:59
  • I don't know anything about Android, but there should be an equivalent expression for an output file that will save it wherever you need it to go. – Jim Garrison Aug 18 '11 at 18:01
  • Well, just like how Java has folders (like src, where the Java file goes), Android programs just have a different folder called "resources", which is where I would like to save the file to. Does this help at all or would you still not know how to go about this? – Mxyk Aug 18 '11 at 18:02
  • What is the path to the resources folder? That's what you'd put in place of `c:\\myDirectory\\myOutput.xml` – Jim Garrison Aug 18 '11 at 18:05
  • Aha! It does indeed save there now, although it doesn't show up in the project right away. Well, you've been a great help, so thank you :-)! – Mxyk Aug 18 '11 at 18:13
0

This page has some great examples of how to serialize the DOM object to a neatly formatted XML file.

Community
  • 1
  • 1
Mike Thomsen
  • 36,828
  • 10
  • 60
  • 83
  • This seems to be more or less just how to print the XML file, which my code already does. I'm wondering how to _store_ it. – Mxyk Aug 18 '11 at 17:50