0

Trying to edit the value of maxTreeAgeInit="50.0" in an xml file (outline as follows)

middle of my xml file (xml version="1.0" encoding="utf-8") of interest:

<InitTreeF treeExistsInit="true" maxTreeAgeInit="50.0" avgTreeAgeInit="50.0" tInitStem="Frac" stemMInitF="" branMInitF="" barkMInitF="" leafMInitF="" cortMInitF="" firtMInitF="" stemVolInitF="" stemFracInitF="0.422069892" branFracInitF="0.199533585" barkFracInitF="0.113706229" leafFracInitF="0.059503198" cortFracInitF="0.17649553" firtFracInitF="0.028691566" stemNCRatioInitF="" branNCRatioInitF="" barkNCRatioInitF="" leafNCRatioInitF="" cortNCRatioInitF="" firtNCRatioInitF="" storNMInitF="" stemM3PGInit="" foliM3PGInit="" rootM3PGInit="" nStemsInit="" frFracInit="1.0" tFrFracInit="FracConst" treeNmInit="Eucalyptus Tall Open Forest" agRegimeId="943207214" />
z <- c("InitTreeF treeExistsInit="true" maxTreeAgeInit="50.0"")

y <- c("InitTreeF treeExistsInit="true" maxTreeAgeInit="60.0"")

xml_1 <- str_replace(stri_read_lines("Southern_NSW_prescribed_1000yr.plo"), z, y)

The R script I am using flags errors e.g. for z : 'unexpected token 'true', unexpected token '50.0'

BIG PICTURE: I wish to modify the xml file parameter values, which is input into an external cmd model.bat I call from a batch file in windows, so I can run the model.bat multiple times with different parameter values.

The R script I am using (below) flags errors e.g. for z : 'unexpected token 'true', unexpected token '50.0'

z <- c("InitTreeF treeExistsInit="true" maxTreeAgeInit="50.0"")

y <- c("InitTreeF treeExistsInit="true" maxTreeAgeInit="60.0"")

xml_1 <- str_replace(stri_read_lines("Southern_NSW_prescribed_1000yr.plo"), z, y)

I'm thinking that I need to escape 'true' and '50' to read the line from the xml file in as text string and then the rest of the R script should work... as it does work on text file with simple text without ="true" or ="50" in it.

alistaire
  • 42,459
  • 4
  • 77
  • 117
pepdave
  • 1
  • 1
  • flip the outer quotes for single quotes or put a backslash before them to escape them – alistaire Oct 18 '19 at 04:40
  • that works! Thank you ;) – pepdave Oct 18 '19 at 06:08
  • Never try to manipulate raw XML as plain text. Always put it through a real XML parser first. Ask yourself, for example: what will your code do if the text it is looking for is in part of the input that has been commented out? The only time this doesn't apply is if you are doing a one-off edit that will never be repeated on different XML files. – Michael Kay Oct 18 '19 at 08:17
  • @MichaelKay my aim is to edit an original xml file and make a copy with a different filename, then use that new xml (txt file in xml format) file as input into a model.exe program that I run using a windows batch file. I will always edit the original xml file... so I'm not sure about using parse or the benefit in doing so unless it ensures the correct format of xml. – pepdave Oct 20 '19 at 01:13
  • If you parse and serialize using proper XML tools then you will always have well-formed XML. If you try to do-it-yourself then there will always be XML inputs that you handle incorrectly, and there will always be outputs you produce that are not well-formed XML. We spend our lives on StackOverflow dealing with the messes that people get themselves (and their clients) into by making this mistake, – Michael Kay Oct 20 '19 at 08:46
  • ok, thanks @MichaelKay ...I'm investigating the XML package in R and hope this package includes 'proper XML tools' that you refer to. I've generated a dataframe from xml file and am now wondering how to convert a dataframe back into xml format, such that my windows model.exe will read it as input correctly. So I am exploring that. Any tips greatly appreciated. – pepdave Oct 21 '19 at 23:49
  • R is one of dozens of technologies on my "must take a look at this" list. – Michael Kay Oct 22 '19 at 07:37

0 Answers0