user1476286
user1476286

Reputation: 119

How to read key and value from xml file using delimiters in java?

I want to read the following xml <LINE> tag using java. Could you please help with this? Thanks in advance.

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

Upvotes: 1

Views: 381

Answers (2)

Mateusz Kowalczyk
Mateusz Kowalczyk

Reputation: 2066

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);
}

Upvotes: 2

Kazekage Gaara
Kazekage Gaara

Reputation: 15052

Situation specific solution would be :

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);

Upvotes: 1

Related Questions