Cobra
Cobra

Reputation: 63

Java xml parsing?

I am trying to read xml files. I can read and load xml files correctly. I just don't understand how do I read this,

<c c="6" r="182, 0, 192, 15" />

Ps: I am on phone, so dont know how to code format

Edit: the r is, x, y, width and height. Edit: is there any method in java xml that can read the r string as four different int or I have to do it manualy?

Thank you.

Upvotes: 0

Views: 135

Answers (3)

user3608828
user3608828

Reputation: 52

Yes, you have to parse it in some way.

Upvotes: 0

James Anderson
James Anderson

Reputation: 27478

You have a tag "c" --
Element maytag = yourdom.getElementByTagName("c");

Which has attributes:

"c" with a value of "6" 

String six = mytag.getAttribute("c");

"r" with a value of "182, 0, 192, 15"

String numlist = mytag.getAttribute("r");

After that you need some plain old Java to split up the string and parse the integers (see Elliot Frisch's answer).

Upvotes: 0

Elliott Frisch
Elliott Frisch

Reputation: 201399

Based on your comment (for splitting the integer values out of a single CSV list in an XML attribute), you could do it with something like -

String str = "182, 0, 192, 15";
String[] values = str.split(", ");
if (values.length >= 4) {
    int x = Integer.parseInt(values[0].trim());
    int y = Integer.parseInt(values[1].trim());
    int width = Integer.parseInt(values[2].trim());
    int height = Integer.parseInt(values[3].trim());
    System.out.printf("x = %d, y = %d, width = %d, height = %d\n", x,
            y, width, height);
}

Upvotes: 1

Related Questions