0

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.

6
  • Whats the problem? It is a well-formed XML file. Commented May 16, 2014 at 20:45
  • 3
    I can read and load xml files correctly. Then what are you asking? Commented May 16, 2014 at 20:46
  • I am trying to read the r, which is four coordinate, x,y, width and height, which I am trying to assign on four int. Commented May 16, 2014 at 20:47
  • 1
    "r" is an attribute of "c". Are you getting the String ("182, 0, 192, 15") back? Commented May 16, 2014 at 20:48
  • Yes, but these are four different values, how do I assign them to four different int. Commented May 16, 2014 at 20:51

3 Answers 3

1

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); } 
Sign up to request clarification or add additional context in comments.

1 Comment

Oh ok, sure, I will do.
0

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

Comments

0

Yes, you have to parse it in some way.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.