Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static String encodeXML(String inXML) {
        Pattern p1 = Pattern.compile("<");
        Matcher m1 = p1.matcher(inXML); // get a matcher object
        StringBuffer sb1 = new StringBuffer();
        while (m1.find()) {
            m1.appendReplacement(sb1, "&lt;");
        }
        inXML = m1.appendTail(sb1).toString();

        Pattern p2 = Pattern.compile(">");
        Matcher m2 = p2.matcher(inXML); // get a matcher object
        StringBuffer sb2 = new StringBuffer();
        while (m2.find()) {
            m2.appendReplacement(sb2, "&gt;");
        }
        String outXML = m2.appendTail(sb2).toString();

        return outXML;
    }
}