0

I'm looking to read from a XML file which is stored within a zip file. I'm having TONs of issues understanding how to do this. The best I seem to be able to do is get an input stream working, BUT I CAN'T UNDERSTAND HOW TO READ IT! So I know that its the right file, no I want to pass the data to a XML parser I have. However I don't really understand what the input stream is, or how to manipulate it properly. I've seen it used before with a buffer and then reading into the buffer, my question with this is how do we properly choose the size of the buffer if it is a sufficiently large file?

Any help would be great, thanks!

ZipFile zf;
    try {
        zf = new ZipFile(directory);
        CharBuffer charBuffer = CharBuffer.allocate(BUFFER_SIZE);
      for (Enumeration<? extends ZipEntry> e = zf.entries();
            e.hasMoreElements();) {
        ZipEntry ze = e.nextElement();
        String name = ze.getName();
        if (name.endsWith(".xml")) {
          InputStream in = zf.getInputStream(ze);
          // read from 'in
        }
      }
    } catch (IOException e1) {
        System.out.println("Sorry we couldn't find the file");
        e1.printStackTrace();
    }

3 Answers 3

2

You mean as in this?

DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document document = docBuilder.parse(in);

Or do you mean you already have a specific XML parser that doesn't take an InputStream - what does it take?

Sign up to request clarification or add additional context in comments.

2 Comments

What library is Document from? I'm having some problems with it.
I think org.w3c.dom.Document
1

You can either use SAX or StAX to parse/read the stream - Reference JAXP

With Stream API,

 XMLStreamReader reader=XMLInputFactory.newFactory().createXMLStreamReader(in);
 while(reader.hasNext())
 {
  System.out.println(reader.next());
  System.out.println(reader.getLocalName());
 }

With SAX,

SAXParserFactory.newInstance().newSAXParser().parse(in, new YourHandler());

Comments

0

A simple google search can lead you to tons of articles explaining the code work involved to parse XML. Why not just search then?

One of them: http://www.java-samples.com/showtutorial.php?tutorialid=152

I'm not including how to unzip a file because you've mentioned that you're through with that, but if you're looking for options I'll edit my answer.

Happy Java Coding. :)

2 Comments

XML parsing is the easy part, how do I get my inputStream to convert to an XML? I literally don't know what to do from the inputstream only.
You need to know about the input stream first, in which format is the inputStream? Then study about the format, learn which bits mean what and the basic data parsing approaches for that particular type of data stream. @jQwierdy

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.