Read data from a txt file, try matching with a regular expression and store it as XML (in a xml file) in JAVA

How to form regular expressions in Java so as to be able to directly store the matched data as XML?

Answers


I would do that using the Scanner class wrapping your text file. And then, create a Pattern object with the appropriate regular expression. Then, you'll be able to provide the Pattern object to the Scanner instance and locate each targeted data. Finally, in order to save these data as XML, I would use JAXB to limit memory footprint as you may not know beforehand how many data you'll have to save as XML.


use something like

public CharSequence fromFile(String filename) throws IOException {
        FileInputStream input = new FileInputStream(filename);
        FileChannel channel = input.getChannel();

        // Create a read-only CharBuffer on the file
        ByteBuffer bbuf = channel.map(FileChannel.MapMode.READ_ONLY, 0, (int)channel.size());
        CharBuffer cbuf = Charset.forName("8859_1").newDecoder().decode(bbuf);
        return cbuf;
    }

and compare as:

try {
    // Create matcher on file
    Pattern pattern = Pattern.compile("pattern");
    Matcher matcher = pattern.matcher(fromFile("yourFile.txt"));

    // Find all matches
    while (matcher.find()) {
        // Get the matching string
        String match = matcher.group();
    }
} catch (IOException e) {
}

Need Your Help

How robust is make?

linux make robustness

How robust is the make utility?

std::map, pointer to map key value, is this possible?

c++ stl pointers map

std::map<std::string, std::string> myMap;

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.