Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java中使用分隔符从xml文件中读取键和值?_Java_Xml - Fatal编程技术网

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

如何在java中使用分隔符从xml文件中读取键和值?,java,xml,Java,Xml,我想使用java阅读以下xml标记。你能帮个忙吗?提前谢谢 <?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> 002:OR,004:0001002:01002:01007:SCEM_02000:007:59000002:KG,002:PC;/ 特定情

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

<?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> 

002:OR,004:0001002:01002:01007:SCEM_02000:007:59000002:KG,002:PC;/

特定情况的解决方案是:

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);
String str=“002:OR,004:0001002:01002:01007:SCEM_02000:,007:59000002:KG,002:PC;/”;
str=str.substring((str.indexOf(“>”)+1),str.lastIndexOf(“
import java.util.regex.*);
公共字符串提取行(字符串xmlSource){
模式p=Pattern.compile((.*);
Matcher m=p.Matcher(xmlSource);
布尔查找=m.find();
如果(!找到)
返回null;
返回m组(1);
}

如何阅读?如果仅此而已,请从文件中读取该行内容。永远不要使用正则表达式解析XML,除非可能是您打算运行一次然后扔掉的代码。例如,如果开始标记中的“>”前面有空格,此代码将失败。向下投票。
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);
}