如何使用java中的分隔符从xml文件中读取键和值?

时间:2012-06-23 02:59:48

标签: java xml

我想使用java阅读以下xml <LINE>标记。你能帮忙吗?提前致谢。

<?xml version="1.0" encoding="UTF-8" ?> 
<LINE>002:OR,004:0001,002:01,002:01,007:SCEM_02,000:,007:590,000,002:KG,002:PC;/</LINE> 

2 个答案:

答案 0 :(得分:2)

import java.util.regex.*;

public String extractLine(String xmlSource) {
    Pattern p = Pattern.compile("<LINE>(.*?)</LINE>");
    Matcher m = p.matcher(xmlSource);
    boolean found = m.find();
    if (!found)
        return null;
    return m.group(1);
}

答案 1 :(得分:1)

特定情况的解决方案是:

String str = "<LINE>002:OR,004:0001,002:01,002:01,007:SCEM_02,000:,007:590,000,002:KG,002:PC;/</LINE> ";
str = str.substring((str.indexOf(">")+1),str.lastIndexOf("<"));
System.out.println(str);