首页 \ 问答 \ 如何使用XML :: Writer perl插入XML(How to insert XML using XML::Writer perl)

如何使用XML :: Writer perl插入XML(How to insert XML using XML::Writer perl)

我试图建立一个可以根据输入的内容动态创建xml的系统。 我有一个总是相同的seleton,并且唯一需要动态创建的是formfields。 因此,例如,我需要将$ testXML插入到我的$ writer XML中。 这是我到目前为止所拥有的

use strict;
use warnings;
use XML::Writer;
my $test = XML::Writer->new(OUTPUT => 'self', DATA_MODE => 1, DATA_INDENT => 2, );
$test->startTag('FormField', name => 'CheckBox1');
$test->startTag('Value');
$test->characters('');
$test->endTag('Value');
$test->endTag('FormField');
my $testXML = $test->end();

插入

my $writer = XML::Writer->new(OUTPUT => 'self', DATA_MODE => 1,           DATA_INDENT => 2, );
$writer->startTag('Section', name => 'FormSectionOne');
$writer->startTag('FormField', name => 'Person1');
$writer->startTag('Value');
$writer->characters('Bob Test');
$writer->endTag('Value');
$writer->endTag('FormField');
$testXML;
my $xml = $writer->end();
print $xml;

现在使用$ writer-> raw($ testXML); 插入,但格式有问题

 <Section name="FormSectionOne">
      <FormField name="Person1">
        <Value>Bob Test</Value>
      </FormField><FormField name="CheckBox1">
  <Value>test</Value>


Im trying to make a system that can dynamically create xml based on what is entered. I have a seleton that will always be the same, and the only thing that need to be created dynamically is the formfields. So for example, I need to insert $testXML into my $writer XML. Here is what I have so far

use strict;
use warnings;
use XML::Writer;
my $test = XML::Writer->new(OUTPUT => 'self', DATA_MODE => 1, DATA_INDENT => 2, );
$test->startTag('FormField', name => 'CheckBox1');
$test->startTag('Value');
$test->characters('');
$test->endTag('Value');
$test->endTag('FormField');
my $testXML = $test->end();

Inserted into

my $writer = XML::Writer->new(OUTPUT => 'self', DATA_MODE => 1,           DATA_INDENT => 2, );
$writer->startTag('Section', name => 'FormSectionOne');
$writer->startTag('FormField', name => 'Person1');
$writer->startTag('Value');
$writer->characters('Bob Test');
$writer->endTag('Value');
$writer->endTag('FormField');
$testXML;
my $xml = $writer->end();
print $xml;

Now using $writer->raw($testXML); inserts but the formatting has problems

 <Section name="FormSectionOne">
      <FormField name="Person1">
        <Value>Bob Test</Value>
      </FormField><FormField name="CheckBox1">
  <Value>test</Value>


原文:https://stackoverflow.com/questions/39820452
更新时间:2023-09-12 14:09

最满意答案

您可以找到value="0"的元素的数量,并将其与总计数进行比较以hide()与类points__fees最接近的元素:

var count = $('.points__fees__input[value="0"]').filter(function(){
  return this.value === "0";
}).get();
if($('.points__fees__input[value]').length == count.length)
  $('.points__fees__input[value="0"]').closest('.points__fees').hide();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="points__fees">
    <div class="points__fees__label">Fees / Charges</div>
    <div class="points__fees__value">
        <div class="points__fees__value__item" data-type="star$">
            <span class="points__fees__text">0</span> Star $
            <input class="points__fees__input" value="0" type="hidden">
        </div>
        <div class="points__fees__value__item" data-type="diamond$">
            <span class="points__fees__text">0</span> Diamond $
            <input class="points__fees__input" value="0" type="hidden">
        </div>
        <div class="points__fees__value__item" data-type="gold$">
            <span class="points__fees__text">0</span> Gold $
            <input class="points__fees__input" value="0" type="hidden">
        </div>
    </div>
</div>


You can find the count of elements with value="0" and compare that with total count to hide() the closest element with class points__fees:

var count = $('.points__fees__input[value="0"]').filter(function(){
  return this.value === "0";
}).get();
if($('.points__fees__input[value]').length == count.length)
  $('.points__fees__input[value="0"]').closest('.points__fees').hide();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="points__fees">
    <div class="points__fees__label">Fees / Charges</div>
    <div class="points__fees__value">
        <div class="points__fees__value__item" data-type="star$">
            <span class="points__fees__text">0</span> Star $
            <input class="points__fees__input" value="0" type="hidden">
        </div>
        <div class="points__fees__value__item" data-type="diamond$">
            <span class="points__fees__text">0</span> Diamond $
            <input class="points__fees__input" value="0" type="hidden">
        </div>
        <div class="points__fees__value__item" data-type="gold$">
            <span class="points__fees__text">0</span> Gold $
            <input class="points__fees__input" value="0" type="hidden">
        </div>
    </div>
</div>

相关问答

更多
  • 通常,您询问如何检测元素外部的单击。 这个问题在这里得到了很好的回答。 另外,这里有一个小提示,显示如何处理隐藏和重新显示您的用例。 我真正添加的唯一一件事就是显示用于重新显示用户名/密码div的任何按钮/方法都需要使用相同类型的event.stopPropagation()来避免重新隐藏您要显示的内容。 HTML:
  • 你可以做: $(obj) //Select the object .parent() //Select the parent of the object .children() //Select all the children of the parent .not(':first-child') //Unselect the first child .remove(); ...
  • 要隐藏一个子元素,你需要一个像这样的结构: #parent:hover .yourchild { display:none; } 其中#parent是外部div并且具有:hover动作,那么您只需匹配子元素以隐藏它。 在这种情况下,我猜你有这样的结构:
    One
    Two
    Three
    然后为了隐藏孩子,你可以这样做: .fullwrap:hover :nth-ch ...
  • 尝试这个: $('.collapse').on('click',function(e){ e.stopPropagation() $(this).parent('.expand').stop().animate({ width: '300px', height:'50px' }); $(this).hide(); }); 在这里演示 您的代码无效,因为单击内部子div ,也会单击父div 。 我们已经停止使用event.stopP ...
  • 你可以检查每个div,如果它的父母有超过3个孩子: var sum = 0; $("div > div > div").each(function () { if($(this).parent().children('div').length >3){ sum += parseInt($(this).text(), 10); } }); console.log(sum);