Adding xml element with attribute to logback.xml #1125
-
Hi all, Am using yq to adjust YAML and in same project I have to update some xml. yq has xml support... Nice!!! But, i don't get a simple (??) add to work.... logback.xml:
i want to simply add so that I end up with:
Am quite sure the solution must be simple, but after hours of google-search and trying anything and everything (e.g. snippet merge into file ... which also does not function correctly!) my only resolve is to ask the REAL experts :) Lotsa thx in advance! P.S. Got remove to work with something like:
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 7 replies
-
Hi - sorry for the late response. So when yq parses this xml, 'appender-ref' is an array (as it appears multiple times). All you need to do is add another item to that array. If it helps (it did for me) first keep the output as yaml (omit the Regardless: yq '.configuration.root.appender-ref += {"+ref": "YETANOTHERLOG"}' sample.xml -p=xml -o=xml |
Beta Was this translation helpful? Give feedback.
-
@mikefarah i'm wondering if maybe there's a regression with this? I am trying to do this with a pom.xml to add a developer to an existing list of developers <project>
<developers>
<developer>
<id>existing</id>
</developer>
</developers>
</project> when I use your solution <project>
<developers>
<developer>
<id>existing</id>
</developer>
<+developer>
<id>new</id>
</+developer>
</project> If I remove the <project>
<developers>
<developer>
<id>new</id>
</developer>
</developers>
</project> any ideas? |
Beta Was this translation helpful? Give feedback.
-
I'm running into a similar issue. My use case is that I would like to programmatically add a suppression field to a list of suppressions in an xml file intended for consumption by a Dependency Check tool. GIVEN I have an xml file with the following shape:
WHEN I run the following command: THEN I get the following incorrect result, where the array is seen as an object field and replaces the key:
This also is broken when you have the correctly formatted json that you want to simply convert to xml:
WHEN I run the following command: THEN I get the following incorrect result:
WHEN I run this same command, but select a different output format (yml for example), I get the correct array output:
|
Beta Was this translation helpful? Give feedback.
Hi - sorry for the late response.
So when yq parses this xml, 'appender-ref' is an array (as it appears multiple times). All you need to do is add another item to that array.
If it helps (it did for me) first keep the output as yaml (omit the
-o=xml
) to get an understanding of what happening under the hood.Regardless:
yq '.configuration.root.appender-ref += {"+ref": "YETANOTHERLOG"}' sample.xml -p=xml -o=xml