Change attribute name of object property

Hi there,

I’m looking for a way to change some XML output on my map. Specifically, I’m looking to change the way the nesting works for an object’s properties.

What is currently output from my XML data is:

<objectgroup name="Object Layer 3" visible="0">
  <object id="89" gid="7" x="576" y="1008" width="144" height="144">
   <properties>
    <property name="duration" value="1"/>
    <property name="offset" value="0"/>
   </properties>
  </object>
 </objectgroup>

What I’d like to output:

<objectgroup name="Object Layer 3" visible="0">
  <object id="89" gid="7" x="576" y="1008" width="144" height="144">    
      <properties duration="1" offset="0"/>
      </properties>
   </object>
</objectgroup>

Is there any way that I can make these changes automatically apply to my XML file?

Thanks! :smile:

P.S.: For some reason my code isn’t indenting. Sorry! :disappointed:

You’d have to fork Tiled and change the code writing the properties in mapwriter.cpp and mapreader.cpp to achieve what you want. It should not be that complicated, but note that Tiled allows any characters to be part of the property name. If you use your alternative suggestion, you’ll need to make sure they are valid XML attribute names.

Why do you want to do this?

Seems like it was because you didn’t put the ``` on their own line.

Heya @bjorn,

Thanks for your response! :smile: The reason for my wanting to change the formatting may be due to a lack of understanding of Xml, but I’ve been getting a little frustrated with the way the data appears. I’m importing my Tiled map into Unity using C#. When trying to serialize the information, the code requires a lot of extra steps.

Two examples of the problems I’m having lately:

  1. There are a lot of nested XmlElements that I have to go through to make sure all of my Xml data is being serialized properly. In the below example, if I want to reach the property attribute “duration”, I need to serialize the Map, ObjectGroup, Object, Properties, and Property elements. Then I’m able to go into the attribute I’m looking for. It’s not a deal breaker or anything, but it’s definitely a longer process than I’d like.

  2. The individual properties create extra work to ensure that the name and the value match up in my code. Instead of being able to serialize “duration” and set it’s value to 1, I have to go into each property, check the name and also check it’s value. I feel that it would be easier to list each attribute in the “properties” element. This way, I can find all of the information I need right there.

<object id="89" gid="7" x="576" y="1008" width="144" height="144">
  <properties>
    <property name="duration" value="1"/>
    <property name="offset" value=".75"/>
  </properties>
</object>

I do appreciate you letting me know where this code can be changed! These small things aren’t deal breakers, rather than quality of life improvements. I’m not sure that I’ll be making any changes any time soon, but I’ll definitely look into it in the future to see how I can optimize for myself. :smiley_cat:

Thanks again for your help!

Usually you first convert the data from XML into some other structure that is useful for your purposes. In C#, I guess you’d use a Dictionary for the properties. This should be a simple loop over the property children of the properties element in your map loader, after which you can stop worrying about whatever structure the XML was in and just look things up from the dictionary you associated with each object.

Btw, “serializing” is the other direction, for example moving data into XML, JSON or some other type of byte array. I think you just meant “looking up”.