duangan1945 2015-11-21 04:03
浏览 47
已采纳

Golang xml不会解组

Im trying to unmarshal the following code that has the go playground at the bottom. The data.DocumentType always returns 0 and should return the inner xml 4. Can someone tell me why this doesnt work?

package main

import (
    "encoding/xml"
    "fmt"

)

type doc struct {
    XMLName       xml.Name `xml:"ownershipDocument"`
    SchemaVersion string   `xml:",innerxml"`
    DocumentType  int      `xml:",innerxml"`
}

func main() {
    var Data doc
    xml.Unmarshal([]byte(VV), &Data)
    fmt.Println(Data.DocumentType)

}

const VV = `<?xml version="1.0"?>
<ownershipDocument>

    <schemaVersion>X0306</schemaVersion>

    <documentType>4</documentType>

    <periodOfReport>2015-11-18</periodOfReport>

    <issuer>
        <issuerCik>0000820081</issuerCik>
        <issuerName>CAMBREX CORP</issuerName>
        <issuerTradingSymbol>CBM</issuerTradingSymbol>
    </issuer>

    <reportingOwner>
        <reportingOwnerId>
            <rptOwnerCik>0001227832</rptOwnerCik>
            <rptOwnerName>KORB WILLIAM B</rptOwnerName>
        </reportingOwnerId>
        <reportingOwnerAddress>
            <rptOwnerStreet1>1 MEADOWLANDS PLAZA</rptOwnerStreet1>
            <rptOwnerStreet2></rptOwnerStreet2>
            <rptOwnerCity>EAST RUTHERFORD</rptOwnerCity>
            <rptOwnerState>NJ</rptOwnerState>
            <rptOwnerZipCode>07073</rptOwnerZipCode>
            <rptOwnerStateDescription></rptOwnerStateDescription>
        </reportingOwnerAddress>
        <reportingOwnerRelationship>
            <isDirector>1</isDirector>
            <isOfficer>0</isOfficer>
            <isTenPercentOwner>0</isTenPercentOwner>
            <isOther>0</isOther>
        </reportingOwnerRelationship>
    </reportingOwner>

    <nonDerivativeTable>
        <nonDerivativeTransaction>
            <securityTitle>
                <value>Common Stock</value>
            </securityTitle>
            <transactionDate>
                <value>2015-11-18</value>
            </transactionDate>
            <transactionCoding>
                <transactionFormType>4</transactionFormType>
                <transactionCode>S</transactionCode>
                <equitySwapInvolved>0</equitySwapInvolved>
            </transactionCoding>
            <transactionAmounts>
                <transactionShares>
                    <value>10000</value>
                </transactionShares>
                <transactionPricePerShare>
                    <value>50.18</value>
                    <footnoteId id="F1"/>
                </transactionPricePerShare>
                <transactionAcquiredDisposedCode>
                    <value>D</value>
                </transactionAcquiredDisposedCode>
            </transactionAmounts>
            <postTransactionAmounts>
                <sharesOwnedFollowingTransaction>
                    <value>36562</value>
                </sharesOwnedFollowingTransaction>
            </postTransactionAmounts>
            <ownershipNature>
                <directOrIndirectOwnership>
                    <value>D</value>
                </directOrIndirectOwnership>
                <natureOfOwnership>
                    <value></value>
                </natureOfOwnership>
            </ownershipNature>
        </nonDerivativeTransaction>
    </nonDerivativeTable>

    <footnotes>
        <footnote id="F1">The price reported in Column 4 is a weighted average price. These shares were sold in multiple transactions at prices ranging from $50.00 to $50.58 inclusive. The reporting person undertakes to provide to Cambrex Corporation, any security holder of Cambrex Corporation, or the staff of the Securities and Exchange Commission, upon request, full information regarding the number of shares sold at each separate price within the range set forth in this Footnote 1 to this Form 4.</footnote>
    </footnotes>

    <remarks></remarks>

    <ownerSignature>
        <signatureName>Samantha Hanley for William B. Korb by POA</signatureName>
        <signatureDate>2015-11-20</signatureDate>
    </ownerSignature>
</ownershipDocument>`

http://play.golang.org/p/nJbAF0zKSJ

  • 写回答

2条回答 默认 最新

  • douan5151 2015-11-21 04:47
    关注

    Remove the innerxml tag on DocumentType, and give it the name of the element to to match (xml:"documentType"). From the xml library docs:

    If the struct has a field of type []byte or string with tag ",innerxml", Unmarshal accumulates the raw XML nested inside the element in that field. The rest of the rules still apply.

    You don't need to store unprocessed XML. You're looking for the value of the field, so you don't need the tag.

    Working version

    *edit: For that matter, the ",innerxml" tag will also break the SchemaVersion field as well—without a matching element (and since SchemaVersion is a string), it stores the whole document.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序