开发者

XML coordinates to variable

I would like to get div position from xml xml look like this:

<coord>140X 120Y</coord>

i have stored both of them in one variable

Coor开发者_StackOverflow中文版d=(x[i].getElementsByTagName("coord")[0].childNodes[0].nodeValue);

i would like to separate X in one variable and Y in another, so i could set position for my div if you have link to some tutorial, which could help me, I would be grateful


Assuming you've got the element data in a string:

String XmlCord = "140X 120Y";

String[] Numbers = XmlCord.Split(new char[] {'X', ' ', 'Y'}, StringSplitOptions.RemoveEmptyEntries);

int X, Y;

if (int.TryParse(Numbers[0], out X) == false)
{
    // handle error
}
if (int.TryParse(Numbers[1], out Y) == false)
{
    // handle error
}

I miss sscanf from C++.

You might be better off storing the data as attributes, something like:

<coord X="140" Y="120" ></coord>


Try using JavaScript's regular expressions, matching groups in particular. For example:

var coord = "140X 120Y"
  , regex = /^(\d+)X\s+(\d+)Y$/
  , m = coord.match(regex), x, y;
if (m) {
  x = Number(m[1]); // => 140
  y = Number(m[2]); // => 120
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜