开发者

How can I capture a Java String from a DOM element?

NOTE: edited by @drachenstern, and I don't mind removing my answer on account of this. The OP is Russian (I believe) Ukrainian (with my apologies) and is having an ESL issue describing his problem. Between the two HRs is his original post.


order that would make getElementsByTagName ("s"); and give value = '2d331be47563423424abdb7fe2eee6401c7b00417'

my string:

<html>
  <head>
    <meta http-equiv='content-type' content='text/html; charset=windows-1251' />
  </head>
  <body onload="try { document.getElementById('l').submit(); } catch(e) { document.getElementsByTagName('form')[0].submit(); }">
    <form id='l' method='post' action='http://vkontakte.ru/login.php'>
      <input type='hidden' name='s' value='2d331be47563423424abdb7fe2eee6401c7b00417' />
            <input type='hidden' name='op' value='slogin' />
      <input type='hidden' name='redirect' value='1' />
      <input type='hidden' name='expire' value='0' />
      <input type='hidden' name='to' value='' />
    </form>
  </body>
</html>

I need Convert String to DOM model order that would make parsing

I write this is my String in Java!

String mystr = "<html....</html>";

Russian - "У меня есть строка в JAVA она содержит в себе html файл(страницу сайта). Мне нужна функция которая достанет value в input. Я знаю что это можно прекрасно сделать через DOM модель, но у меня не получается это сделать."

DocumentBuilderFactory factory = DocumentBuilderFactory.n开发者_如何学编程ewInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
InputSource is = new InputSource( new StringReader( body ) );
Document d = builder.parse( is );
String ss = d.getElementById("s").getNodeValue(); //- null

He understands the way to parse for an element in Javascript is via something like getElementsByTagName('s'); and he wants to find the equivalent way to do this in Java. He doesn't know what library he needs. For what it's worth, I don't write Java either. He can only explain one metaphor with another tho.

Google translates his Russian request as:

I have a string in JAVA it contains the html file (page). I need a function that would get value in the input. I know that this can be done well through the DOM model, but I did not get to do it.


It looks like the problem is that he is calling getElementById where the element has name='s' NOT id='s'. I would suggest calling getElementsByTagName instead and then looping through the returned list of input nodes to find what is needed.


If your HTML string will not be any more complicated than this, then the easiest solution is just to parse with a Regular Expression, like

name='s' value='(.*?)'
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜