Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/

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

public class Main {
    public static int getTagIntData(String xmlStr, String xmlRoot) {
        String tags[] = getTagDataArray(xmlStr, xmlRoot);
        if (tags.length > 0) {
            String intStr = tags[0];
            return (Integer.valueOf(intStr)).intValue();
        }
        return 0;
    }

    public static String[] getTagDataArray(String xmlStr, String xmlRoot) {
        List<String> data = new ArrayList<String>();
        Matcher m;
        m = Pattern.compile("<" + xmlRoot + ">(.*?)</" + xmlRoot + ">", Pattern.MULTILINE | Pattern.DOTALL)
                .matcher(xmlStr);
        while (m.find()) {
            data.add(xmlToText(m.group(1)));
        }
        return data.toArray(new String[data.size()]);
    }

    /**
     * Given xml strings containing xml reserved characters, replace with displayable characters > <= & gt; < <= & lt; &
     * <= & amp; ' <= & apos; " <= & quot;
     */
    public static String xmlToText(String xml) {
        if (xml == null || xml.equals("")) {
            return "";
        }
        String str = xml;
        str = str.replaceAll("&gt;", ">");
        str = str.replaceAll("&lt;", "<");
        str = str.replaceAll("&apos;", "'");
        str = str.replaceAll("&quot;", "\"");
        str = str.replaceAll("&amp;", "&");
        return str;
    }
}