how to set html node value using webview with javafx - java

i am trying to set the value of the html form elements after loaded into webview.I tried to set using
org.w3c.dom.Document doc = webEngine.getDocument();
HTMLFormElement form = (HTMLFormElement) doc.getElementsByTagName("form").item(0);
NodeList nodes = form.getElementsByTagName("input");
nodes.item(1).setNodeValue("yadayada"); //this is where i am setting the value
but no success. can anybody help me out. here is my code.
org.w3c.dom.Document doc = webEngine.getDocument();
if (doc!=null && doc.getElementsByTagName("form").getLength() > 0) {
HTMLFormElement form = (HTMLFormElement) doc.getElementsByTagName("form").item(0);
String username = null;
String password = null;
NodeList nodes = form.getElementsByTagName("input");
for (int i = 0; i < nodes.getLength(); i++) {
if(nodes.item(i).hasAttributes()){
NamedNodeMap attr = nodes.item(i).getAttributes();
for (int j=0 ; j<attr.getLength();j++){
Attr atribute = (Attr)attr.item(j);
if(atribute.getValue().equals("password")){
System.out.println("Password detected");
nodes.item(i).setNodeValue("123456");
}
}
}
}
}

i found the solution after surfing the web. The problem was i was using set node value but values of input tags are set using HTMLInputElement.This link was valuabe for me
Performing an automated form post of login using webview
for example
HTMLInputElement password = (HTMLInputElement) nodes.item(0).setValue("yadayada");

Related

Finding attribute from .getNamedItem returns null after parsed xml

I'm currently working for the first time with API's and are having some trouble retrieving data.
The xml file looks like this:
<schedule>
...
<scheduledepisode>
<episodeid>22441</episodeid>
<title>Ekonyheter </title>
<starttimeutc>2012-09-19T04:00:00Z</starttimeutc>
<endtimeutc>2012-09-19T04:03:00Z</endtimeutc>
<program id="83" name="Ekot" />
<channel id="164" name="P3" />
</scheduledepisode>
<scheduledepisode>
So i used NodeList nodelist1 = doc.getElementsByTagName("scheduledepisode"); to get all the scheduledepisode elements, then I thought that to revive the data under title I could simple use the following:
System.out.println(node.getAttributes().getNamedItem("title").getTextContent());
However this only returns null and i cant understand why, can someone explain what I am missing here. To my understanding the title element is a attribute to the scheduledepisode element. Is that wrong?
The length of the nodelist is correct contra the amount of scheduledepisodes so I'm assuming that I have gotten the correct elements.
The code looks like this:
NodeList nodelist1 = doc.getElementsByTagName("scheduledepisode");
for (int i = 0; i < nodelist1.getLength(); i++)
{
Node node = nodelist1.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE )
{
if (node.getAttributes().getNamedItem("title") != null) {
System.out.println(node.getAttributes().getNamedItem("title").getTextContent());
}
}
}
Since <title> is an element and not attribute of <scheduledepisode>, getAttributes() would not work. Therefore, use getElementsByTagName again:
NodeList se_nodelist = doc.getElementsByTagName("scheduledepisode");
for (int i = 0; i < nodelist1.getLength(); i++)
{
Node node = nodelist1.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE )
{
if (node.getElementsByTagName("title") != null) {
System.out.println(node.getElementsByTagName("title").item(0).getTextContent());
}
}
}

java html parser multi page table

i am using Jsoup as html parser to get all the details from the table in this website. With the code below am only able to get the data on the first page only. Any advise?
public static void main(String[] args) {
String html = "http://www.fifa.com/worldranking/rankingtable/index.html#";
try {
Document doc = Jsoup.connect(html).get();
Elements tableElements = doc.select("table");
Elements tableHeaderEles = tableElements.select("thead tr th");
System.out.println("headers");
System.out.print("row");
for (int i = 0; i < tableHeaderEles.size(); i++) {
System.out.print(tableHeaderEles.get(i).text() + " | ");
}
System.out.println();
Elements tableRowElements = tableElements.select(":not(thead) tr");
for (int i = 0; i < tableRowElements.size(); i++) {
Element row = tableRowElements.get(i);
System.out.print("row");
Elements rowItems = row.select("td");
for (int j = 0; j < rowItems.size(); j++)
{
System.out.print(rowItems.get(j).text() + " | ");
}
System.out.println();
}
} catch (IOException e) {
e.printStackTrace();
} }
JSoup is a HTML parser, but looking at the website is using javascript to load the table. So you will need to click into it.
You could use HTMLUnit or Selenium for navigate and JSoup to parse the HTML.
I hope it helps.
Edit:
Looking better in the code of the page. I think that it could be useful :
http://www.fifa.com/worldranking/rankingtable/gender=m/rank=100/confederation=0/page=0/_ranking_table.html
I change the values of the URL, look that the rank u can increase (is the date of the ranking) and the important one would be the page. You could load all the ranking increasing the page parameter. Then just parsing it with JSoup would be enough.
For example the last ranking would be:
http://www.fifa.com/worldranking/rankingtable/gender=m/rank=237/confederation=0/page=1/_ranking_table.html
Then you could increase the parameter page=2, then 3, ... till 7
Cheers.

How do I check for empty tags while parsing xml?

I am using the Document object to extract all the tags from an xml. If the xml has an empty tag, I get a null pointer exception. How do I guard against this? How do I check for an empty tag?
<USTrade>
<CreditorId>
<CustomerNumber>xxxx</CustomerNumber>
<Name></Name>
<Industry code="FY" description="Factor"/>
</CreditorId>
<DateReported format="MM/CCYY">02/2012</DateReported>
<AccountNumber>54000</AccountNumber>
<HighCreditAmount>0000299</HighCreditAmount>
<BalanceAmount>0000069</BalanceAmount>
<PastDueAmount>0000069</PastDueAmount>
<PortfolioType code="O" description="Open Account (30, 60, or 90 day account)"/>
<Status code="5" description="120 Dys or More PDue"/>
<Narratives>
<Narrative code="GS" description="Medical"/>
<Narrative code="CZ" description="Collection Account"/>
</Narratives>
</USTrade>
<USTrade>
So, when I use:
NodeList nm = docElement.getElementsByTagName("Name");
if (nm.getLength() > 0)
name = nullIfBlank(((Element) nm.item(0))
.getFirstChild().getTextContent());
Nodelist gives a length of 1, because there is a tag, but when I do getTextContent(), it hits the null pointer because FirstChild() doesn't return anything for tag = Name
And, I have done this for each xml tag. Is there a simple check I can do before every tag extraction?
The first thing I would do would be to unchain your calls. This will give you the chance to determine exactly which reference is null and which reference you need to do a null check for:
NodeList nm = docElement.getElementsByTagName("Name");
if (nm.getLength() > 0) {
Node n = nm.item(0);
Node child = n.getFirstChild();
if(child == null) {
// null handling
name = null;
}
else {
name = nullIfBlank(child.getTextContent());
}
}
Also, check out the hasChildNodes() method on Node! http://docs.oracle.com/javase/1.4.2/docs/api/org/w3c/dom/Node.html#hasChildNodes%28%29
while(current != null){
if(current.getNodeType() == Node.ELEMENT_NODE){
String nodeName = current.getNodeName();
System.out.println("\tNode: "+nodeName);
NamedNodeMap attributes = current.getAttributes();
System.out.println("\t\tNumber of Attributes: "+attributes.getLength());
for(int i=0; i<attributes.getLength(); i++){
Node attr = attributes.item(i);
String attName = attr.getNodeName();
String attValue= attr.getNodeValue();
System.out.println("\t\tAttribute Name: "+ attName+ "\tAttribute Value:"+ attValue);
}
}
Are you also wanting to print out the value of the node? If so, it's one line of code in my example you would have to add, and I can share that as well.
Did you tried something like that?
NodeList nm = docElement.getElementsByTagName("Name");
if ((Element) nm.item(0))
name = nullIfBlank(((Element) nm.item(0)).getFirstChild().getTextContent());

java android very large xml parsing

I have got a very large xml file with categories in one xml file which maps to sub categories in another xml file according to category id. The xml file with only category id and names is loading fast, but the xml file which has subcategories with images path, description, latitude-longitude etc...is taking time to load.
I am using javax.xml package and org.w3c.dom package.
The list action is loading the file in each click to look for subcategories.
Is there any way to make this whole process faster?
Edit-1
Heres the code i am using to getch subcategories:
Document doc = this.builder.parse(inStream, null);
doc.getDocumentElement().normalize();
NodeList pageList = doc.getElementsByTagName("page");
final int length = pageList.getLength();
for (int i = 0; i < length; i++)
{
boolean inCategory = false;
Element categories = (Element) getChild(pageList.item(i), "categories");
if(categories != null)
{
NodeList categoryList = categories.getElementsByTagName("category");
for(int j = 0; j < categoryList.getLength(); j++)
{
if(Integer.parseInt(categoryList.item(j).getTextContent()) == catID)
{
inCategory = true;
break;
}
}
}
if(inCategory == true)
{
final NamedNodeMap attr = pageList.item(i).getAttributes();
//
//get Page ID
final int categoryID = Integer.parseInt(getNodeValue(attr, "id"));
//get Page Name
final String categoryName = (getChild(pageList.item(i), "title") != null) ? getChild(pageList.item(i), "title").getTextContent() : "Untitled";
//get ThumbNail
final NamedNodeMap thumb_attr = getChild(pageList.item(i), "thumbnail").getAttributes();
final String categoryImage = "placethumbs/" + getNodeValue(thumb_attr, "file");
//final String categoryImage = "androidicon.png";
Category category = new Category(categoryName, categoryID, categoryImage);
this.list.add(category);
Log.d(tag, category.toString());
}
}
Use SAX based parser, DOM is not good for large xml.
Maybe a SAX processor would be quicker (assuming your App is slowing down due to memory requirements of using a DOM-style approach?)
Article on processing XML on android
SOF question about SAX processing on Android

how can i get data out of DIV using html parser in java

i am using Java html parser(link text) to try to parse this line.
<td class=t01 align=right><div id="OBJ123" name=""></div></td>
But I am looking for the value like I see on my web browser, which is a number. Can you help me get the value?
Please let me know if you need more details.
Thanks
From the documentation, all you have to do is find all of the DIV elements that also have an id of OBJ123 and take the first result's value.
NodeList nl = parser.parse(null); // you can also filter here
NodeList divs = nl.extractAllNodesThatMatch(
new AndFilter(new TagNameFilter("DIV"),
new HasAttributeFilter("id", "OBJ123")));
if( divs.size() > 0 ) {
Tag div = divs.elementAt(0);
String text = div.getText(); // this is the text of the div
}
UPDATE: if you're looking at the ajax url, you can use similar code like:
// make some sort of constants for all the positions
const int OPEN_PRICE = 0;
const int HIGH_PRICE = 1;
const int LOW_PRICE = 2;
// ....
NodeList nl = parser.parse(null); // you can also filter here
NodeList values = nl.extractAllNodesThatMatch(
new AndFilter(new TagNameFilter("TD"),
new HasAttributeFilter("class", "t1")));
if( values.size() > 0 ) {
Tag openPrice = values.elementAt(OPEN_PRICE);
String openPriceValue = openPrice.getText(); // this is the text of the div
}

Categories

Resources