简体   繁体   English

xml/html 格式化和输出

[英]Xml / html formatting and output

Hi I'm still newish to javascript / html / xml, but do understand the concepts and can do some basic coding.嗨,我对 javascript / html / xml 还很陌生,但确实了解这些概念并且可以进行一些基本的编码。

I'm trying to get some xml data, and put this data into some html tables, this all works, but the data is not being put onto separate table rows.我正在尝试获取一些 xml 数据,并将这些数据放入一些 html 表中,这一切正常,但是数据没有被放到单独的表行中。 I understand I may have to create arrays ,but the code I have is via the net and I'm stuck on how to to do this.我知道我可能必须创建数组,但我拥有的代码是通过网络获得的,我一直在思考如何做到这一点。 So I'm looking for some help around javascript/ html.所以我正在寻找有关 javascript/html 的帮助。

It's working but the display does not look right, as it's all on one line per header, and I need it to be on separate rows.它正在工作,但显示看起来不正确,因为每个标题都在一行上,我需要它在不同的行上。

I've googled, and tried various options, but with no luck.我用谷歌搜索,并尝试了各种选择,但没有运气。

The javascript function: javascript函数:

function myGetEventsXmlData() {
    $.ajax({
        type: "GET",
        url: "data/event_list.xml",
        dataType: "xml",
        success: xmlEventParser
    });
}

function xmlEventParser(xml) {
$(xml).find('event_list').each(function () {
    var $show = $(this);
    var data = {
        title: $show.find('title').text(),
        name: $show.find('name').text(),
        severity: $show.find('severity').text(),

    };
    var row = $('<tr />');
    for (var prop in data) {
        $('<td>' + data[prop] + '</td>').appendTo(row);
    }

    $('#eventtable').append(row);
});
}  

This is the xml file im trying to parse这是我试图解析的 xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<event_list>
    <event>
        <id>9199acca-df09-71e6-1946-c0a800820000</id>
        <title>client.createConnection(691) - AMQ214016: Failed to create netty connection </title>
        <name>test1.system.com</name>
        <state>open</state>
        <severity>critical</severity>
        <priority>medium</priority>
        <category>Internal</category>
     </event>
     <event>
         <id>9199acca-df09-71e6-1946-c0a800820000</id>
         <title>ssh login failed </title>
         <name>test2.system.com</name>
         <state>open</state>
         <severity>major</severity>
         <priority>medium</priority>
         <category>OS</category>
     </event>
     <event>
         <id>9199acca-df09-71e6-1946-c0a800820000</id>
         <title>Oracle Connection OK </title>
         <name>test3.system.com</name>
         <state>open</state>
         <severity>normal</severity>
         <priority>medium</priority>
         <category>Database</category>
     </event>
 </event_list>

html code section html代码部分

              <table id="eventtable" table=table style="width:100%"               align="center">
            <tr>
                <th>Title</th>
                <th>Name</th>
                <th>Severity</th>

            </tr>
        </table>

The data appears in the section but its all together and not seperate rows, what I'd like is for the data to be on a new row for each new data, rather than on the one row.数据出现在该部分中,但它全部放在一起而不是单独的行,我希望数据在每个新数据的新行上,而不是在一行上。

Any thoughts on how to do this would be great.关于如何做到这一点的任何想法都会很棒。

$(xml).find('event_list').each(function () {

You are looping over each <event_list> (of which you have only one).您正在遍历每个<event_list> (其中只有一个)。

You should be looping over each <event> .您应该遍历每个<event>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM