使用XPath从Web解析XML,传递给HTML表单

时间:2014-07-02 01:11:45

标签: java html xml xpath

我在运行XPath脚本时遇到了困难。我正在使用DocumentBuilderFactory来存储服务器上其他网站的XML数据。我的逻辑和语法对我来说是正确的,但脚本不会运行。

以下是完整HTML的链接:

https://www.dropbox.com/s/i3u429bn5bjd4i9/orecalculator.txt

    <script language="JavaScript">

import java.net.URL;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.*;
import org.w3c.dom.Document;

    URL url = new URL("http://api.eve-central.com/api/marketstat?typeid=22,17425,17426,1223,17428,17429,1225,17432,17433,1232,17436,17437,1229,17865,17866,21,17440,17441,1231,17444,17445,1226,17448,17449,20,17452,17453,11396,17869,17870,1227,17867,17868,18,17455,17456,1224,17459,17460,1228,17463,17464,19,17466,17467,1230,17470,17471&regionlimit=10000002");
    Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(url.openStream());
    XPath xpath = XPathFactory.newInstance().newXPath();

var IDs = [22,17425,17426,1223,17428,17429,1225,17432,17433,1232,17436,17437,1229,17865,17866,21,17440,17441,1231,17444,17445,1226,17448,17449,20,17452,17453,11396,17869,17870,1227,17867,17868,18,17455,17456,1224,17459,17460,1228,17463,17464,19,17466,17467,1230,17470,17471];

calculate = function (i)
{
    var y = document.getElementById('reward'); //to display in reward box
    var x = 0;  
    XPathExpression expr;
    Object result;
    var elm;
    while (i + 1 < IDs.length)
    {
        expr = xpath.compile("//marketstat/type[@id = '" + IDs[i] + "']/buy/max/text()"); //define XPath for each typeID
        result = expr.evaluate(document);
        elm = document.getElementById(IDs[i]);
        x=x+parseInt(elm.value||0)*parseInt(result); //multiply the max buy price for this typeID by the number of units entered in the html form
        i++;
    }
    y.value = x;
};

</script>

脚本的目的是将每个表单的用户输入乘以相关的&#34; max buy&#34;从前夕中心XML获取该项目的ID。它应该像这样的Google表格:

https://docs.google.com/spreadsheets/d/1iLxDHFQOfAsiFPSUr0bU1sXyE37dfUdVAXc1xoSzFCc/edit?usp=sharing

谢谢!

1 个答案:

答案 0 :(得分:0)

您不能将Java放在HTML页面内的Javascript script标记内,并期望它能够运行。 为此,您必须在Javascript中重写代码,或者创建一个完整的Java Web应用程序。