javascript - 如何使用JQuery获取xml中指定标签的直接子标签

标签 javascript jquery xml children xml-documentation

请检查以下代码。我只需要检查父标签内定义的子项。

如果该父项中出现任何意外的子项,我需要将其报告为错误。

这是我的 XML 文件:

<places>
  <Tirunelveli>XXXX</Tirunelveli>//allowed child of places
  <Tiruchendur></Tiruchendur>//allowed child of places
  <Alwar></Alwar>//allowed child of places
  <sweet></sweet>//not allowed child of places and i have to report this tag as error
</places>

我需要检查 <places> parent 只有允许的 child 标签。否则我需要将其添加为错误。

var rd = new FileReader();
rd.onload = function(e){  
var xmlDoc = $.parseXML(this.result);                   
var $xml = $(xmlDoc);
//check allowed child of front tag
check_allowed_direct_child("places", "Tirunelveli,Tiruchendur,Alwar", "RULE_002", "Fail");                  

//Function to check the x tag have only the alowed child y
function check_allowed_direct_child(x,y,rule,error_type)
{
    var child_array = y.split(',');
    var child_count=child_array.length;
    var ischild=""
    var xmlchild="";
    $xml.children(x).each(function()
    {
        ischild="no";
        xmlchild=this.value;
        for(i=0;i<count;i++)
        {
        if(child_array[i]==xmlchild)
        {
            ischild="yes";
        }
        }
        if(ischild=="no")
        {
            //count the total error and warnings
            check_total_error_warning(error_type);
            $("#validation_report").append('<tr class="err"><td><a href="Asset\Rules\Rule.html\#'+rule+'">'+rule+'</td><td><span class="highlight">&#x0003C;'+xmlchild+'&#x0003E;</span> is not allowed inside the <span class="highlight">&#x0003C;'+x+'&#x0003E;</span> element</td><td class="'+classname+'">'+error_type+'</td></tr>');
            }

            });
            }

};rd.readAsText(this.files[i]);

但是 children()代码不工作。我做错了什么?

最佳答案

您选择父节点的方式需要更正:使用 find 找到它,然后对该结果使用 children(不带参数)来获取所有子节点。

如果父节点是 XML 的根,则 find 将找不到它,但是使用 addBack 您还可以在匹配中包含根节点。

我还会为您的函数名称使用驼峰式命名,并使用 jQuery 函数构建动态 HTML。

这是它的样子:

var $xml = $("<aa><bb></bb><cc></cc><dd></dd><ee></ee></aa>");
var className = "someClass"
 
//check allowed child of front tag
checkChildAllowed($xml, "aa", ["bb","cc","dd"], "RULE_002", "Fail");                   

//Function to check the x tag have only the allowed child y
function checkChildAllowed($xml, parent, allowedChildren, rule, errorType) {
    // Make sure to first locate the parent correctly:
    var $parent = $xml.find(parent).addBack(parent); 
    // Uppercase tag names as jQuery uses that HTML convention
    allowedChildren = allowedChildren.map(childName => childName.toUpperCase()); 
    $parent.children().each(function () {
        if (allowedChildren.includes(this.nodeName)) return; // tag is OK
        // Include this if that function is defined:
        //check_total_error_warning(error_type); 
        $("#validation_report").append( // Use jQuery functions
            $("<tr>").addClass("err").append(
                $("<td>").append(
                    $("<a>").attr("href", "Asset\Rules\Rule.html\#"+rule).text(rule)
                ),
                $("<td>").append(
                    $("<span>").addClass("highlight")
                               .text("<"+this.nodeName.toLowerCase()+">"),
                    " is not allowed inside the ",
                    $("<span>").addClass("highlight").text("<"+parent+">"),
                    " element"
                ),
                $("<td>").addClass(className).text(errorType)
            )
        );
    });
}
table { border-collapse: collapse }
td { border: 1px solid; padding: 5px }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="validation_report"></table>

关于javascript - 如何使用JQuery获取xml中指定标签的直接子标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52696201/

相关文章:

javascript - 使用 window.getSelection() 检查文本是否突出显示

javascript - IE-9 将占位符的值作为值

jquery - html css将div定位在其他元素下方而不调整html

javascript - XML/javascript 解析节点 - 按名称指定子节点

android - 如何在android xml资源文件中保留URL?

c# - 如何使用等效于 C# .NET 的#region/#endregion(大纲)来组织 xml 数据

php - 如何使用 Zend_Date 简单地更改时区偏移量?

javascript - 动态创建的元素上的事件绑定(bind)?

java - 为每个新添加的项目启动一个分钟计数器

javascript - requestAnimFrame 堆栈溢出 PIXI js