利用正则表达式解析简单XML

人生的第二篇CSDN博客,还是好激动啊~

package homework;

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

/**
 * Created by Administrator on 2016/8/2.
 */

  /*
        解析XML语句,利用list存储解析出的Shu的对象:

        当group(0)为空时说明是标签开始时,如果是Books说明是一组新的
        组,所以new一个储存结果的列表arraylist储存Shu,当为Book时创建一个Shu对象放入临时栈stack,判断group(3),如果不为空
        说明是这种情况,直接将stack中未被设置过内容的Shu弹出放入list结果集.
        当group(0)不为空时,说明是标签的结束,如果是Book将临时栈顶的Shu弹出并放入最终list,如果是Name或者Price则对
        栈顶的对象调用peek()修改内容

         */

public class JiexiShu {
    public static void main(String[] args) {
        String str = "\n" +
                "    \n" +
                "        Java入门\n" +
                "        30.00\n" +
                "    \n" +
                "    \n" +
                "        Java基础\n" +
                "        130.00\n" +
                "    \n" +
                "    \n" +
                "        Java精通\n" +
                "        256.00\n" +
                "    \n" +
                "    \n" +
                "        Java进阶\n" +
                "        998.00\n" +
                "    \n" +
                "    \n" +
                "        程序员之禅\n" +
                "        122.00\n" +
                "    \n" +
                "    \n" +
                "        颈椎指南\n" +
                "        50.00\n" +
                "    \n" +
                "    \n" +
                "        活着\n" +
                "        26.00\n" +
                "    \n" +
                "    \n" +
                "";



        List list = null;
        Stack stack = new Stack<>();
        Stack last = new Stack<>();
        Pattern pattern=Pattern.compile("<(/?)([^<>/]+)(/?)>");
        Matcher matcher=pattern.matcher(str);
        while (matcher.find()){
            if(matcher.group(1).isEmpty()){
                last.push(matcher.end());
                switch (matcher.group(2)){
                    case "Books":
                        list=new ArrayList<>();
                        break;
                    case "Book":
                        stack.push(new Shu());
                        break;

                }
                if (!matcher.group(3).isEmpty()){
                    last.pop();
                    switch (matcher.group(2)){
                        case "Book":
                            list.add(stack.pop());
                            break;
                    }
                }
            }else {
                switch (matcher.group(2)){
                    case "Name":
                        stack.peek().setName(str.substring(last.pop(),matcher.start()));
                        break;
                    case "Price":
                        stack.peek().setId(str.substring(last.pop(),matcher.start()));
                        break;
                    case "Book" :
                        list.add(stack.pop());
                        break;
                }
            }
        }
        for (Shu shu : list) {
            System.out.println(shu);
        }
    }
}

class Shu:

package homework;

/**
* Created by Administrator on 2016/8/2.
*/
public class Shu {
private String name;

private String id;
public Shu() {
}
public Shu(String name, String id) {
    this.name = name;
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

@Override
public String toString() {
    return "Shu{" +
            "name='" + name + '\'' +
            ", id='" + id + '\'' +
            '}';
}

}

你可能感兴趣的:(JAVA)