-4

I want to add variables to my file so it can be read by another programme. Each variable name needs to be unique.

I've tried searching lots on Stack Overflow but none of the answers solve my problem in one go and I'm too inexperienced to glue them all together. The solution can be with an XML parser or just with .txt files. Languages I would prefer in order: Python, Java, C# or Scala. It doesn't need to be a robust/long term solution as it won't be relied upon.

E.g.

<Tag1>
     <USA></USA>
     <UK></UK>
</Tag1>
<Tag2>
     <FRA></FRA>
     <USA></USA>
</Tag2>

I want to edit the above file into:

<Tag1>
     <USA>var1a</USA>
     <UK>var1b</UK>
     <FRA>var1c</FRA>
</Tag1>
<Tag2>
     <USA>var2a</USA>
     <UK>var2b</UK>
     <FRA>var2c</FRA>
</Tag2>

Thanks

  • 1
    welcome to stackoverflow. what have you ***tried yourself*** so far? what problems did you encounter? what have you researched? i recommend [taking the tour](https://stackoverflow.com/tour), as well as reading [how to ask a good question](https://stackoverflow.com/help/how-to-ask) and [what's on topic](https://stackoverflow.com/help/on-topic). – Franz Gleichmann May 06 '21 at 09:35

2 Answers2

0

Using xml linq :

using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication
{
     class Program
    {
         static void Main(string[] args)
        {
            string xml = @"<Root>
                              <Tag1>
                                <USA></USA>
                                <UK></UK>
                              </Tag1>
                              <Tag2>
                                <FRA></FRA>
                                <USA></USA>
                              </Tag2>
                            </Root>";

            XDocument doc = XDocument.Parse(xml);

            List<XElement> tags = doc.Descendants().Where(x => x.Name.LocalName.StartsWith("Tag")).ToList();
            List<string> countries = tags.SelectMany(x => x.Elements().Select(y => y.Name.LocalName)).Distinct().OrderBy(x => x).ToList();

            for (int i = 0; i < tags.Count; i++)
            {
                List<XElement> children = countries.Select((x, j) => new XElement(x, "var" + (i + 1).ToString() + ((char)(((byte)'a') + j)).ToString())).ToList();
                tags[i].ReplaceWith(new XElement(tags[i].Name.LocalName, children));

            }
        }
    }
 
}
jdweng
  • 33,250
  • 2
  • 15
  • 20
-2

With Java, you could achieve this using a FileReader and if/then statements. First, import java.io.*

import java.io.*;

Then, you'd write code that looked something like this

File XMLFile =  new File("<filepath here>");
FileReader Reader =  new FileReader(XMLFile);
ArrayList<String> Lines = new ArrayList<String>();
while(Reader.hasNextLine()){
 Lines.add(Reader.NextLine());
}

This would create an ArrayList of Strings where each String is a line of the file. I entrust the rest of the project to you!