Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Root level attributes don't get encoded back to attribute when converting back to XML file from Plist #127

Closed
MehmetBal opened this issue Sep 9, 2019 · 2 comments · Fixed by #134
Assignees
Labels
more info needed Not enough details available to proceed

Comments

@MehmetBal
Copy link

MehmetBal commented Sep 9, 2019

Hi,

First of all thanks for creating such a useful library! I am having a bit of a hard time with root-level attribute encoding.

I have a complex XML that requires a big model but to simply:

To decode something this:

<?xml version="1.0" encoding="UTF-8"?>
<policy name = "..." attr2="...">
   <Initial>
    more xml here
     .....
     ....
  </Initial>
</policy>

I am doing

struct Policy: Codable, Equatable, DynamicNodeEncoding {
    var name: String
    var initial: Initial
enum CodingKeys: String, CodingKey {
        case name,  initial
    }

static func nodeEncoding(for key: CodingKey) -> XMLEncoder.NodeEncoding {
        switch key {
        case CodingKeys.name
            return .attribute
        default:
            return .element
        }
    }
}

Then I decode using XMLDecoder(), encode to PLIST using PropertyListEncoder() then encode back to XML with first PropertyListDecoder() and then

            let data = try encoder.encode(modifiedData, withRootKey: "policy",
                                          header: XMLHeader(version: 1.0,
                                                            encoding: "UTF-8"))

and everything works gets encoded properly except the root level attributes.
The result becomes:

<policy><name>generic</name>
<initial>
   .....
   .....
</initial>
</policy>

I tried .both instead of just encoding as .attribute but that did not make a difference.

Any ideas on how to fix this?

Thanks

MaxDesiatov pushed a commit that referenced this issue Oct 4, 2019
Resolves #127. I think the plist encoding/decoding should be independent of the XML encoding phase. I have tried to reproduce just the relevant encoding phase and it seems to work.
@MaxDesiatov
Copy link
Collaborator

MaxDesiatov commented Oct 4, 2019

Hi @MehmetBal, can you please have a look at #134? We aren't able to reproduce the issue with the test added in that PR. Is there anything missing that should be added to the test to reproduce this?

@MaxDesiatov MaxDesiatov reopened this Oct 4, 2019
@MaxDesiatov MaxDesiatov added the more info needed Not enough details available to proceed label Oct 4, 2019
bwetherfield added a commit to bwetherfield/XMLCoder that referenced this issue Oct 16, 2019
Resolves CoreOffice#127. I think the plist encoding/decoding should be independent of the XML encoding phase. I have tried to reproduce just the relevant encoding phase and it seems to work.
@MaxDesiatov
Copy link
Collaborator

This issue hasn't been updated for more than a few months, I'm assuming it can be closed as outdated.

arjungupta0107 pushed a commit to salido/XMLCoder that referenced this issue Jun 26, 2020
Resolves CoreOffice#127. I think the plist encoding/decoding should be independent of the XML encoding phase. I have tried to reproduce just the relevant encoding phase and it seems to work.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
more info needed Not enough details available to proceed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants