0

Here is a simplistic example for reading xml-file into WebRowSet object and then loading data from it to database.

import javax.sql.rowset.RowSetProvider;
import javax.sql.rowset.WebRowSet;
import javax.sql.rowset.spi.SyncProviderException;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;


public class WebRowSetTest {
    public static void main(String[] args) throws SQLException {

        // Create WebRowSet object and populate it with data from xml-file.
        WebRowSet receiver = RowSetProvider.newFactory().createWebRowSet();
        Path file = Paths.get("priceList.xml");
        try (InputStream in = Files.newInputStream(file)) {
            receiver.readXml(in);
        } catch (IOException x) {
            x.printStackTrace();
        }
        System.out.println("WebRowSet deserialiazed.");

        // Establish connection with database
        String connectionURL = "jdbc:mysql://localhost:3306/testdb";
        Properties connectionProps = new Properties();

        connectionProps.put("user", "root");
        connectionProps.put("password", "1234");
        connectionProps.put("serverTimezone", "Europe/Moscow");
        Connection conn = DriverManager.getConnection(connectionURL, connectionProps);
        conn.setAutoCommit(false);

        // Load data from WebRowSet object to database.
        try {
            receiver.acceptChanges(conn);
        } catch (SyncProviderException spe) {
            System.out.println("You need specify how to resolve the conflict.");
        }

        // Close connection.
        conn.close();
    }
}

There is another method for reading xml-file which uses Reader instead InputStream. So, I could replace the lines of code for reading xml-file into WebRowSet with something like this:

    FileReader fReader = null;
    try {
        fReader = new FileReader("priceList.xml");
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    receiver.readXml(fReader);

But isn't that true, that reading xml-file into WebRowSet object using InputStream is faster than using Reader? If so, then what is the purpose of readXml(Reader reader) in this case?

escudero380
  • 536
  • 2
  • 7
  • 25

1 Answers1

2

One takes an InputStream (byte-oriented) and the other takes Reader (char-oriented). The methods provided are more for convenience. In some cases you have an InputStream, and in other a Reader and being forced to convert to a specific type is cumbersome, while the underlying XML library used by the rowset reference implementation is able to handle either just fine. So offering both is just cheap and convenient.

I'm not sure why you think InputStream would be faster than reader. Which is faster largely depends on the actual type of stream or reader (eg buffered or not). As XML is a character-oriented format, likely using Reader has a minor advantage, but I'd be surprised if that would be a noticeable difference compared to buffered vs unbuffered.

So, in short, the reason that both methods exists is convenience, not performance.

For example, if I already have a string with the value, then constructing a StringReader is more convenient than trying to derive an InputStream using a ByteArrayInputStream.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • Yes, it's all I've been tought from basic I/O tutorial: Readers are more _convenient_ to read text data because with Reader you can handle characters more easily, and IputStreams are for grabbing bytes directly. If I were to implements parsing myself, I would chose a Reader. But here all the work has been already done for me, and the matter of convenience doesn't play any role. In the end I deal with WebRowSet object from where I retrieve the values as strings, floats, booleans, etc... So, I was just curious, which one of these methods works more effectively with the underlined routine. – escudero380 May 22 '19 at 18:14
  • Anyway, does it mean that if I were to read from xml-file located the local disc directory (not from the socket) into WebRowSet object, I should always consider Files.newBufferedReader​(file) over Files.newInputStream(file) as an argument for readXml method? – escudero380 May 22 '19 at 18:40
  • @escudero380 The matter of convenience does play a role when designing an API. This isn't about convenience of the implementer of WebRowSet, this is about convenience for the user of the API (ie: you). And as `Files.newBufferedReader` is buffered and `Files.newInputStream` is not, the choice should be obvious. However, you'd really need to measure the difference to be sure. – Mark Rotteveel May 23 '19 at 08:08