Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Manipulate Value in XML File
#1
Been a while since I've used QM and could use some help finding the right approach for this task:

in a target XML file, I need to find every line that contains <string name="Path" and then change its value from a PC path to a Mac path.

For example, the following line will appear many times in an XML file:

<string name="Path" value="X:\AUDIO PROJECTS\TestFolder\"/>

For each one, I need to
A) replace the PC drive name X: in the value field with its Mac equivalent /Volumes/AUDIO SSD
B) turn all the backslashes in the value field into forward slashes, so each occurrence now looks like this:

<string name="Path" value="/Volumes/AUDIO SSD/AUDIO PROJECTS/TestFolder/"/>

Note: The PC drive name "X:" and replacement Mac equivalent "/Volumes/AUDIO SSD" will always be used in the XMLs I need to process, but the rest of the path will change from XML to XML.

I think I can probably figure it out eventually, but some help getting started would be appreciated. How to find and each line with "<string name="Path"" and load whole line to manipulate, or is there a simple way using QM XML tools to get the value for Path each time it occurs and then I can manipulate its value? Been too long...

Thanks!
#2
Macro Macro2315
Code:
Copy      Help
str s=
;<x>
;<string name="Path" value="X:\AUDIO PROJECTS\TestFolder\"/>
;<z>
;<string name="Path" value="X:\AUDIO PROJECTS\TestFolder2\"/>
;</z>
;</x>

IXml x._create
x.FromString(s)

ARRAY(IXmlNode) a; int i
x.RootElement.GetAll(0 a)
for i 0 a.len
,IXmlNode n=a[i]
,if(StrCompare(n.Name "string")) continue
,s=n.AttributeValue("value")
,if(!s.begi("X:\")) continue
,s.findreplace("\" "/")
,s.replace("/Volumes/AUDIO SSD" 0 2)
,n.Attribute("value").Value=s

x.ToString(_s); out _s

Or use regular expression, but may be not easy to make reliable.
#3
Perfect, thank you sir!


Forum Jump:


Users browsing this thread: 1 Guest(s)