I want to change the lastModifiedBy field from Tom Hanks to Jerry Garcia. I used this repo: https://github.com/clbanning/mxj/blob/master/xml.go to parse the xml bytes into a map. However, some fields were left out.
What's an easy way to change that field and only that field? There are hundreds of these files so I need to do it programmatically.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<cp:coreProperties
xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core-
properties" xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:dcterms="http://purl.org/dc/terms/"
xmlns:dcmitype="http://purl.org/dc/dcmitype/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><dc:title></dc:title>
<dc:subject></dc:subject><dc:creator>John Kerry</dc:creator>
<cp:keywords></cp:keywords><dc:description></dc:description>
<cp:lastModifiedBy>TomHanks</cp:lastModifiedBy><cp:revision>6</cp:revision>
<dcterms:created xsi:type="dcterms:W3CDTF">2018-02-
20T18:08:00Z</dcterms:created><dcterms:modified
xsi:type="dcterms:W3CDTF">2018-04-24T19:43:00Z</dcterms:modified>
</cp:coreProperties>
<cp:lastModifiedBy>JerryGarcia</cp:lastModifiedBy><cp:revision>6</cp:revision>
Delete TomHanks and write JerryGarcia
maybe just this then (easiest thing that works) then after the replace do the xml parsing? Not sure if the TomHanks => Jerry Garcia thing is really always the same thing, or you have to parameterize this.
package main
import (
"fmt"
"strings"
)
var xmlRaw = `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<cp:coreProperties
xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core-
properties" xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:dcterms="http://purl.org/dc/terms/"
xmlns:dcmitype="http://purl.org/dc/dcmitype/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><dc:title></dc:title>
<dc:subject></dc:subject><dc:creator>John Kerry</dc:creator>
<cp:keywords></cp:keywords><dc:description></dc:description>
<cp:lastModifiedBy>TomHanks</cp:lastModifiedBy><cp:revision>6</cp:revision>
<dcterms:created xsi:type="dcterms:W3CDTF">2018-02-
20T18:08:00Z</dcterms:created><dcterms:modified
xsi:type="dcterms:W3CDTF">2018-04-24T19:43:00Z</dcterms:modified>
</cp:coreProperties>`
type decoder struct {
}
func main() {
fmt.Println(strings.Replace(xmlRaw, "TomHanks", "Jerry Garcia", 1))
}
https://play.golang.org/p/viTLElQxesM
this demonstrates the problem I was describing:
package main
import (
"encoding/xml"
"fmt"
)
var xmlRaw = []byte(`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<cp:coreProperties
xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core-
properties" xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:dcterms="http://purl.org/dc/terms/"
xmlns:dcmitype="http://purl.org/dc/dcmitype/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><dc:title></dc:title>
<dc:subject></dc:subject><dc:creator>John Kerry</dc:creator>
<cp:keywords></cp:keywords><dc:description></dc:description>
<cp:lastModifiedBy>TomHanks</cp:lastModifiedBy><cp:revision>6</cp:revision>
<dcterms:created xsi:type="dcterms:W3CDTF">2018-02-
20T18:08:00Z</dcterms:created><dcterms:modified
xsi:type="dcterms:W3CDTF">2018-04-24T19:43:00Z</dcterms:modified>
</cp:coreProperties>`)
type decoder struct {
Keywords string `xml:"keywords"`
LastModifiedBy string `xml:"lastModifiedBy"`
//.. more xml
}
func main() {
d := decoder{}
if err := xml.Unmarshal(xmlRaw, &d); err != nil {
panic(err)
}
fmt.Println(d.LastModifiedBy)
d.LastModifiedBy = "Jerry Garcia"
bytes, err := xml.Marshal(d)
if err != nil {
panic(err)
}
fmt.Println(string(bytes))
}
https://play.golang.org/p/aYa01_3UE5e
Output:
TomHanks
<decoder><keywords></keywords><lastModifiedBy>Jerry Garcia</lastModifiedBy></decoder>