解析不产生预期的产量

时间:2017-02-21 04:25:01

标签: xml parsing groovy

我正在使用Groovy来解析以下文件。我使用的代码部分是在输出结尾处连接人员角色,即使正在循环处理人员角色。

def People = new XmlSlurper().parseText(Sample).
declareNamespace('tptc':'http://www.metadata.calcum-inc.com/stats/')

// Person

People.Person.each
{

    println  it.PersonRole.each{ role->
    println  role.PersonRoleCode
        println  role.PersonRoleDescription
    }
}

以下是我使用的代码部分:

COMMANDER
IN CHARGE OF ARMY
CONDUCTOR
DIRECTS SYMPHONY
COMMANDERIN CHARGE OF ARMYCONDUCTORDIRECTS SYMPHONY
First
Best Best
None
  Nonetity
  FirstBest BestNoneNonetity
  Result: COMMANDERIN CHARGE OF ARMYCONDUCTORDIRECTS    SYMPHONYJackieWangMangStringStringStringStringStringStringStringStringStringStringStringStringFirstBest  BestNoneNonetityStringStringStringStringStringStringStringString
 StringStringStringStringStringMANICString

这是输出:

var styles = {
  "#textContainer": {
    "border": "blue solid 2px;",
    "background-color": "#ff0;"
  },
  "#textContainer > button": {
    "color": "#f00"
  }
};


var newStyle = document.createElement('style');
newStyle.type = "text/css";
newStyle.appendChild(document.createTextNode(getCSS()));

document.querySelector("#textContainer").appendChild(newStyle);

function getCSS() {
  var css = [];
  for (let selector in styles) {
    let style = selector + " {";
    
    for (let prop in styles[selector]) {
      style += prop + ":" + styles[selector][prop];
    }
    
    style += "}";
    
    css.push(style);
  }
  
  return css.join("\n");
}

它连接Person元素的所有其余输出。我只想要它打印我关心的元素,即。循环中的代码。有办法实现吗?请帮忙!

1 个答案:

答案 0 :(得分:0)

这是因为,你的代码之后没有任何东西。

您可以在脚本下面。这个脚本实际上做的是它收集role and description然后最终显示结果。

def xml="""<tptc:People xmlns:tptc="http://www.metadata.calcum-inc.com/stats/">
    <tptc:Identifier>456-99088</tptc:Identifier>
    <tptc:Name>The Grand Supper</tptc:Name>
     <tptc:Person>
     <tptc:PersonRole>
         <tptc:PersonRoleCode>COMMANDER</tptc:PersonRoleCode>
        <tptc:PersonRoleDescription>
      IN CHARGE OF   ARMY    
    </tptc:PersonRoleDescription>
    </tptc:PersonRole>
    <tptc:PersonRole>
        <tptc:PersonRoleCode>CONDUCTOR</tptc:PersonRoleCode>
        <tptc:PersonRoleDescription>
      DIRECTS SYMPHONY
   </tptc:PersonRoleDescription>
    </tptc:PersonRole>
    <tptc:PersonName>
        <tptc:PersonFirstName>Jackie</tptc:PersonFirstName>
        <tptc:PersonMiddleName>Wang</tptc:PersonMiddleName>
        <tptc:PersonLastName>Mang</tptc:PersonLastName>
    </tptc:PersonName>
    <tptc:PersonAddress>
        <tptc:PersonAddressLine1Text>String</tptc:PersonAddressLine1Text>
        <tptc:PersonAddressLine2Text>String</tptc:PersonAddressLine2Text>
        <tptc:PersonCityName>String</tptc:PersonCityName>
        <tptc:PersonStateCode>String</tptc:PersonStateCode>
        <tptc:PersonZip5Code>String</tptc:PersonZip5Code>
        <tptc:PersonZip4Code>String</tptc:PersonZip4Code>
    </tptc:PersonAddress>
    <tptc:PersonTelephone>
        <tptc:PersonAreaCode>String</tptc:PersonAreaCode>
        <tptc:PersonTelephoneNumber>String</tptc:PersonTelephoneNumber>
        <tptc:TelephoneTypeCode>String</tptc:TelephoneTypeCode>
    </tptc:PersonTelephone>
    <tptc:PersonTelephone>
        <tptc:PersonAreaCode>String</tptc:PersonAreaCode>
        <tptc:PersonTelephoneNumber>String</tptc:PersonTelephoneNumber>
        <tptc:TelephoneTypeCode>String</tptc:TelephoneTypeCode>
    </tptc:PersonTelephone>
   </tptc:Person>
   <tptc:Person>
    <tptc:PersonRole>
        <tptc:PersonRoleCode>First</tptc:PersonRoleCode>
        <tptc:PersonRoleDescription>Best Best</tptc:PersonRoleDescription>
    </tptc:PersonRole>
    <tptc:PersonRole>
        <tptc:PersonRoleCode>None</tptc:PersonRoleCode>
        <tptc:PersonRoleDescription>Nonetity</tptc:PersonRoleDescription>
    </tptc:PersonRole>
      <tptc:PersonName>
        <tptc:PersonFirstName>String</tptc:PersonFirstName>
        <tptc:PersonMiddleName>String</tptc:PersonMiddleName>
        <tptc:PersonLastName>String</tptc:PersonLastName>
     </tptc:PersonName>
      <tptc:PersonAddress>
        <tptc:PersonAddressLine1Text>String</tptc:PersonAddressLine1Text>
        <tptc:PersonAddressLine2Text>String</tptc:PersonAddressLine2Text>
        <tptc:PersonCityName>String</tptc:PersonCityName>
        <tptc:PersonStateCode>String</tptc:PersonStateCode>
        <tptc:PersonZip5Code>String</tptc:PersonZip5Code>
        <tptc:PersonZip4Code>String</tptc:PersonZip4Code>
      </tptc:PersonAddress>
       <tptc:PersonTelephone>
        <tptc:PersonAreaCode>String</tptc:PersonAreaCode>
        <tptc:PersonTelephoneNumber>
          String
       </tptc:PersonTelephoneNumber>
        <tptc:TelephoneTypeCode>String
      </tptc:TelephoneTypeCode>
       </tptc:PersonTelephone>
      <tptc:PersonTelephone>
        <tptc:PersonAreaCode>String</tptc:PersonAreaCode>
        <tptc:PersonTelephoneNumber>
             String
      </tptc:PersonTelephoneNumber>
        <tptc:TelephoneTypeCode>
           String
      </tptc:TelephoneTypeCode>
      </tptc:PersonTelephone>
     </tptc:Person>
   </tptc:People>"""

def parsedXml = new XmlSlurper().parseText(xml)
def ppl = parsedXml.'**'.findAll{ it.name() == 'Person'}
def result = []
ppl.each { peop ->
  peop.'**'.findAll{ it.name() == 'PersonRole'}.each { role ->
    def detail = []
    detail << role.PersonRoleCode
    detail << role.PersonRoleDescription.text().trim()
    result << detail
  }
}
println result.toString()

<强>输出:

[[COMMANDER, IN CHARGE OF   ARMY], [CONDUCTOR, DIRECTS SYMPHONY], [First, Best Best], [None, Nonetity]]

基本上是数据列表。由于result变量具有列表形式的详细信息,因此您可以在以后执行任何操作。