4
<emp>
<name>Jhon</name>
<sal>2000</sal>
</emp>

I will get this xml as string.I need to generate an xml file from the string and i need to name the generated xml file with name tag.eg:Jhon.xml.please provide me some pointers to do the same in java with out using parsers.

mohan
  • 87
  • 2
  • 8
  • 2
    http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – Rekin Jul 16 '11 at 08:59
  • But, on the other side: http://stackoverflow.com/questions/4231382/regular-expression-pattern-not-matching-anywhere-in-string/4231482#4231482 – Rekin Jul 16 '11 at 09:00
  • possibly duplicate to: http://stackoverflow.com/questions/335250/parsing-xml-with-regex-in-java – fyr Jul 16 '11 at 09:05

2 Answers2

2

Using string substring or regex is parsing the file. I assume you mean you don't want to have to parse every detail.

If you know each element is on a line by itself you can use the following approach.

BufferedReader br = 
String line;
while((line = br.readLine()) != null) {
    String[] parts = line.split("[<>]");
    String tag = parts[1];
    String value = parts[2];
    if ("name".equals(tag)) {

    } else if ("ruleId".equals(tag)) {

    } else if ("ruleVersion".equals(tag)) {

    }
}
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0

Although caution applies when using regex over xml... this will do it in one line:

String filename = input.replaceAll("(?s).*<name>(.*)</name>.*<ruleId>(.*)</ruleId>.*<ruleVersion>(.*)</ruleVersion>.*", "$1_$2_$3.xml");

The (?s) is important - it turns on the "dot matches newline" switch, so your input can contain multiple lines (ie embedded newlines) but be treated as a single line.

Here's a test of this line you can run:

public static void main(String[] args) throws Exception
{
    String input = "<name>remove use case</name>\n    <ruleId>2161</ruleId>\n    <ruleVersion>0.0.1</ruleVersion>\n    <ruleStatus>New</ruleStatus>\n    <nuggetId>489505737</nuggetId>\n    <icVersionId>50449</icVersionId>\n    <rlVersion>1.0</rlVersion>\n    <modelVersion>1.0</modelVersion>\n    <attributes>\n        <attribute>\n            <attributeName/>\n            <value/>\n        </attribute>\n    </attributes>\n    <notes></notes>";
    String filename = input.replaceAll("(?s).*<name>(.*)</name>.*<ruleId>(.*)</ruleId>.*<ruleVersion>(.*)</ruleVersion>.*", "$1_$2_$3.xml");
    System.out.println(filename);
}

Output:

remove use case_2161_0.0.1.xml
Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • While I'd do a similar approach I wouldn't put everything into one regular expression. Sure, it's a lot shorter that way, but it will also be error prone if some file has the tags in a different order (which wouldn't matter for generic xml parsing). – Mario Jul 16 '11 at 09:31