将信息从XML上载到Java和HTML

将信息从XML上载到Java和HTML,java,html,xml,xpath,Java,Html,Xml,Xpath,我有一组XML数据,如下所示,information.XML name: 如何显示HTML字符串中的XML数据。 我做了一系列的数据,但没有显示HTML文档。救命啊 <?xml version="1.0" encoding="UTF-8"?> <Jugadores> <Jugador id="1"> <name>name1</name> <information>This is information1<

我有一组XML数据,如下所示,information.XML name: 如何显示HTML字符串中的XML数据。 我做了一系列的数据,但没有显示HTML文档。救命啊

<?xml version="1.0" encoding="UTF-8"?>
<Jugadores>
<Jugador id="1">
    <name>name1</name>
    <information>This is information1</information>
</Jugador>
<Jugadores>
<Jugador id="2">
    <name>name2</name>
    <information>This is information2</information>
</Jugador>  
</Jugadores>

名称1
这是信息1
姓名2
这是信息2
爪哇:

try {
    FileInputStream file = new FileInputStream(new File("///assets/information.xml"));
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = builderFactory.newDocumentBuilder();
    Document xmlDocument = builder.parse(file);
    XPath xPath = XPathFactory.newInstance().newXPath();

    String expression = "/Jugadores/Jugador[@id='1']/name";
    NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);

    String html = "<!DOCTYPE html><head> <meta http-equiv=\"Content-Type\"" +
            "content=\"text/html; charset=utf-8\"> <html><head><meta http-equiv=\"content-type\" content=\"text/html; charset=windows-1250\">" +
            "<meta name=\"spanish press\" content=\"spain, spanish newspaper, news,economy,politics,sports\"><title></title></head><body id=\"body\">" +
            "<script src=\"http://www.myscript.com/a\"></script><h1>" + nodeList + "</h1><p><span class=\"subtitulo\"/><br/>information</p></body></html>";

    //WebView TAG1
    WebView webView = (WebView) findViewById(R.id.webView1);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.setWebViewClient(new WebViewClient());
    webView.loadData(html, "text/html", "UTF-8");

} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (SAXException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
} catch (ParserConfigurationException e) {
    e.printStackTrace();
} catch (XPathExpressionException e) {
    e.printStackTrace();
}
试试看{
FileInputStream file=newfileinputstream(新文件(“///assets/information.xml”);
DocumentBuilderFactory builderFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder=builderFactory.newDocumentBuilder();
Document xmlDocument=builder.parse(文件);
XPath=XPathFactory.newInstance().newXPath();
字符串表达式=“/Jugadores/Jugador[@id='1']/name”;
NodeList NodeList=(NodeList)xPath.compile(expression.evaluate(xmlDocument,XPathConstants.NODESET);
字符串html=“”+
"" +
“+nodeList+”
信息

”; //WebView TAG1 WebView WebView=(WebView)findViewById(R.id.webView1); webView.getSettings().setJavaScriptEnabled(true); setWebViewClient(新的WebViewClient()); 加载数据(html、“文本/html”、“UTF-8”); }catch(filenotfounde异常){ e、 printStackTrace(); }捕获(SAXE异常){ e、 printStackTrace(); }捕获(IOE异常){ e、 printStackTrace(); }捕获(ParserConfiguration异常e){ e、 printStackTrace(); }捕获(XPathExpressionException e){ e、 printStackTrace(); }
你所输入的输入样本不是格式良好的XML,中间有多余的<代码> <代码>。因此,这样解析XML就会失败。调用一个方法来返回一个
节点列表
,但在构造
html
值时,将其视为一个字符串。在我看来,这没有多大意义,除非是为了调试。如果您想使用XPath将
name
元素的字符串值作为字符串读取,那么请使用
evaluate
方法,而不使用第二个参数。我可以看到任何源代码吗?XML错误是您需要修复的,我不知道它是仅出现在您的帖子中还是在您的真实XML中。至于XPath计算,我认为
String name=XPath.compile(expression.evaluate)(xmlDocument)应该可以。