Registrierung Kalender Mitgliederliste Teammitglieder Suche Häufig gestellte Fragen Zur Startseite

Informatiker Board » Themengebiete » Praktische Informatik » Softwaretechnik » XML templates konvertieren » Antwort erstellen » Hallo Gast [Anmelden|Registrieren]

Antwort erstellen
Benutzername: (du bist nicht eingeloggt!)
Thema:
Nachricht:

HTML ist nicht erlaubt
BBCode ist erlaubt
Smilies sind erlaubt
Bilder sind erlaubt

Smilies: 21 von 33
smileWinkDaumen hoch
verwirrtAugenzwinkerngeschockt
Mit ZungeGottunglücklich
Forum Kloppebösegroßes Grinsen
TanzentraurigProst
TeufelSpamWillkommen
LehrerLOL HammerZunge raus
Hilfe 
aktuellen Tag schließen
alle Tags schließen
fettgedruckter Textkursiver Textunterstrichener Text zentrierter Text Hyperlink einfügenE-Mail-Adresse einfügenBild einfügen Zitat einfügenListe erstellen CODE einfügenPHP CODE farbig hervorheben
Spamschutz:
Text aus Bild eingeben
Spamschutz

Die letzten 10 Beiträge
hornblower

Das ist Super! Vielen Dank dafür! Die Parameter wie Sailmode etc können im Programm dann noch verändert werden, das ist also nicht so wichtig.
Ihr habt mir damit einen großen Schritt weitergeholfen!

Beste Grüße,

hornblower
Karlito

So, hier erstmal eine Variante. Die Werte müssen sicher noch angepasst werden. Ist nur zusammengehackt und nicht schön (Quick and Dirty). Im Anhang mit Quellen und Projekt (VS2013)

code:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace XMLConverter
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void buttonBrowseSource_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == DialogResult.OK)
                textBoxSource.Text = ofd.FileName;
        }

        private void buttonBrowseDest_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            if (sfd.ShowDialog() == DialogResult.OK)
                textBoxDest.Text = sfd.FileName;
        }

        private void buttonStart_Click(object sender, EventArgs e)
        {
            string xml;
            try
            {
                using (StreamReader sr = new StreamReader(textBoxSource.Text))
                {
                    xml = sr.ReadToEnd();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Fehler beim Lesen der Datei:\n\n" + ex);
                return;
            }

            if (xml.Contains("</RouteModelType>"))
            {
                Format1ToFormat2(xml);
            }
            else
            {
                Format2ToFormat1(xml);
            }
        }

        private void Format2ToFormat1(string xml)
        {
            XmlDocument input = new XmlDocument();
            XmlDocument output = new XmlDocument();

            XmlDeclaration xmlDeclaration = output.CreateXmlDeclaration("1.0", "UTF-8", null);
            XmlElement root = output.DocumentElement;
            output.InsertBefore(xmlDeclaration, root);

            XmlElement routeModelTypeNode = output.CreateElement("RouteModelType");
            XmlAttribute xsdAttribute = output.CreateAttribute("xmlns:xsd");
            xsdAttribute.Value = "http://www.w3.org/2001/XMLSchema";
            XmlAttribute xsiAttribute = output.CreateAttribute("xmlns:xsi");
            xsiAttribute.Value = "http://www.w3.org/2001/XMLSchema-instance";
            routeModelTypeNode.Attributes.Append(xsdAttribute);
            routeModelTypeNode.Attributes.Append(xsiAttribute);
            output.InsertBefore(xmlDeclaration, root);
            output.AppendChild(routeModelTypeNode);

            input.LoadXml(xml);
            string routeName = input.DocumentElement.ChildNodes[0].Attributes[1].Value;

            XmlElement routeNameNode = output.CreateElement("Name");
            routeNameNode.InnerText = routeName;
            routeModelTypeNode.AppendChild(routeNameNode);

            XmlElement notesNode = output.CreateElement("Notes");
            routeModelTypeNode.AppendChild(notesNode);

            foreach (XmlNode node in input.GetElementsByTagName("waypoint"))
            {
                routeModelTypeNode.AppendChild(createWayPoint(output, node));
            }

            try
            {
                output.Save(textBoxDest.Text);
                MessageBox.Show("erledigt!");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Fehler beim Schreiben der Datei:\n\n" + ex);
            }
        }

        private XmlElement createWayPoint(XmlDocument document, XmlNode node)
        {
            XmlElement result = document.CreateElement("Waypoints");
            XmlElement waypointName = document.CreateElement("Name");
            XmlElement latitude = document.CreateElement("Latitude");
            XmlElement longitude = document.CreateElement("Longitude");
            XmlElement notes = document.CreateElement("Notes");
            XmlElement isParameterPoint = document.CreateElement("IsParameterPoint");
            XmlElement sailMode = document.CreateElement("SailMode");
            XmlElement isArrivalPoint = document.CreateElement("IsArrivalPoint");
            XmlElement radius = document.CreateElement("Radius");
            XmlElement trackLimit = document.CreateElement("TrackLimit");
            XmlElement courseLimit = document.CreateElement("CourseLimit");
            XmlElement economy = document.CreateElement("Economy");
            XmlElement maximalSpeed = document.CreateElement("MaximalSpeed");

            waypointName.InnerText = node.Attributes["name"].Value;

            latitude.InnerText = convertCoordinate(node.Attributes["lat"].Value);
            longitude.InnerText = convertCoordinate(node.Attributes["lon"].Value);

            isParameterPoint.InnerText = "false";
            sailMode.InnerText = "0";
            isArrivalPoint.InnerText = "false";
            radius.InnerText = "463";
            trackLimit.InnerText = "1000";
            courseLimit.InnerText = "0";
            economy.InnerText = "0";
            maximalSpeed.InnerText = "0";
            
            result.AppendChild(waypointName);
            result.AppendChild(latitude);
            result.AppendChild(longitude);
            for (int i = 0; i < 5; i++)
            {
                result.AppendChild(i==0?notes:notes.Clone());
            }
            result.AppendChild(isParameterPoint);
            result.AppendChild(sailMode);
            result.AppendChild(isArrivalPoint);
            result.AppendChild(radius);
            result.AppendChild(trackLimit);
            result.AppendChild(courseLimit);
            result.AppendChild(economy);
            result.AppendChild(maximalSpeed);

            return result;
        }

        private string convertCoordinate(String coordinate)
        {
            Double result = Double.Parse(coordinate, CultureInfo.InvariantCulture);
            result *= Math.PI / 180;

            return "" + result.ToString(CultureInfo.InvariantCulture);
        }

        private void Format1ToFormat2(string xml)
        {
            xml = xml.Remove(0, xml.IndexOf("<Name>") + 6);
            string name = xml.Substring(0, xml.IndexOf("</Name>"));
            StringBuilder output = new StringBuilder();
            output.AppendLine("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            output.AppendLine();
            output.AppendLine("<sposroutetemplate xmlns=\"http://www.meteogroup-maritime.com/spos/routetemplate\" xmlVersion=\"1\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">");
            output.AppendLine();
            output.AppendLine();
            output.AppendLine("<template created=\"2015-02-23T14:13:58.5077114Z\" name=\"" + name + "\">");
            output.AppendLine();
            output.AppendLine();
            output.AppendLine("<waypoints>");
            output.AppendLine();
            while (xml.IndexOf("</Waypoints>") > 0)
            {
                xml = xml.Remove(0, xml.IndexOf("<Name>") + 6);
                name = xml.Substring(0, xml.IndexOf("</Name>"));
                xml = xml.Remove(0, xml.IndexOf("<Latitude>") + 10);
                double latitude = double.Parse(xml.Substring(0, xml.IndexOf("</Latitude>")), CultureInfo.InvariantCulture) * 180 / Math.PI;
                xml = xml.Remove(0, xml.IndexOf("<Longitude>") + 11);
                double longitude = double.Parse(xml.Substring(0, xml.IndexOf("</Longitude>")), CultureInfo.InvariantCulture) * 180 / Math.PI;
                xml = xml.Remove(0, xml.IndexOf("</Waypoints>") + 12);
                output.AppendLine("<waypoint name=\"" + name + "\" ignoreLand=\"false\" routeTemplatePointType=\"Fixed\" tracktype=\"RhumbLine\" useSpeed=\"false\" speed=\"0\" delay=\"PT0S\" lon=\"" + longitude.ToString(CultureInfo.InvariantCulture) + "\" lat=\"" + latitude.ToString(CultureInfo.InvariantCulture) + "\"/>");
                output.AppendLine();
            }
            output.AppendLine("</waypoints>");
            output.AppendLine();
            output.AppendLine("</template>");
            output.AppendLine();
            output.AppendLine("</sposroutetemplate>");
            try
            {
                using (StreamWriter sw = new StreamWriter(textBoxDest.Text))
                    sw.Write(output.ToString());
                MessageBox.Show("erledigt!");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Fehler beim Schreiben der Datei:\n\n" + ex);
            }
        }
    }
}


Gruß,

Karlito

Dateianhang:
zip InformatikerBoard.zip (106 KB, 402 mal heruntergeladen)
Karlito

Problem ist hier wieder, dass sich gewisse Werte nich in der Ursprungs-XML finden. Da wäre die Frage, welche Standardwerte angenommen werden sollen. (Radius, SailMode, Tracklimit...)
Karlito

Muss denn der Typ erkannt werden? Das Einfachste und Sicherste wäre die Konvertierungsrichtung anzugeben.

Die Umwandlung in die Rückrichtung müsste dann speziell implementiert werden. Das ist jedoch kein großer Aufwand.

Edit: Die automatische Erkennung des Typs ist auch kein Problem... Es kann daran entschieden werden, welches das Wurzelelement der XML-Datei ist.

Gruß,

Karlito
hornblower

Moin Moin,

ich habe doch nochmal eine Frage.. wie viel Aufwand wäre es, das Programm so umzuschreiben, dass der Prozess umkehrbar wird? Sprich man einen Dateityp 2 in einen Dateityp 1 umwandelt?
Im Prinzip ist das ja dadurch, dass im Programm alles direkt umgeschrieben wird nicht einfach umkehrbar, sondern es müsste ein neues Schema geschrieben und am Anfang irgendwie eine Erkennung um welche Datei es sich handelt eingebracht werden, oder?

Beste Grüße,

Hornblower
hornblower

Alles klar, dann werde ich das so machen!
Vielen Dank nochmal!
Das Programm ist so perfekt, es geht nur darum, dass die Positionsangaben mit den Wegpunktnamen nicht alle manuell eingegeben werden müssen.
Beste Grüße,

hornblower
eulerscheZahl

Klar, kannst du machen.
Als Quelle würde ich einfach http://www.informatikerboard.de/board/thread.php?threadid=2177 angeben.

Da der Aufbau der Eingangsdatei recht starr ist und sich nur die Anzahl der Punkte sowie deren Werte ändern, habe ich einfach den string zerlegt.
Wenn man komplexere Strukturen analysieren will, gibt es dafür fertige Parser. Die nehmen einem dann die Arbeit des Auslesens ab und ermöglichen es, die einzelnen Knoten abzuarbeiten.
hornblower

Das ist der Wahnsinn wie das funktioniert! Vielen Dank!

Das Programm würde ich gerne in meiner Diplomarbeit erwähnen. Wäre das ok für euch? Und welche Quellenangaben kann ich dafür verwenden?

Beste Grüße,

hornblower
eulerscheZahl

Ich dachte, das hätte ich rausgelöscht...

Naja, hier nochmal mit GUI, die Version sollte es jetzt tun.
Bedienung sollte sich von selbst erklären, hoffe ich.

code:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace XMLConverter
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void buttonBrowseSource_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == DialogResult.OK)
                textBoxSource.Text = ofd.FileName;
        }

        private void buttonBrowseDest_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            if (sfd.ShowDialog() == DialogResult.OK)
                textBoxDest.Text = sfd.FileName;
        }

        private void buttonStart_Click(object sender, EventArgs e)
        {
            string xml;
            try
            {
                using (StreamReader sr = new StreamReader(textBoxSource.Text))
                {
                    xml = sr.ReadToEnd();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Fehler beim Lesen der Datei:\n\n" + ex);
                return;
            }
            xml = xml.Remove(0, xml.IndexOf("<Name>") + 6);
            string name = xml.Substring(0, xml.IndexOf("</Name>"));
            StringBuilder output = new StringBuilder();
            output.AppendLine("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            output.AppendLine();
            output.AppendLine("<sposroutetemplate xmlns=\"http://www.meteogroup-maritime.com/spos/routetemplate\" xmlVersion=\"1\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">");
            output.AppendLine();
            output.AppendLine();
            output.AppendLine("<template created=\"2015-02-23T14:13:58.5077114Z\" name=\"" + name + "\">");
            output.AppendLine();
            output.AppendLine();
            output.AppendLine("<waypoints>");
            output.AppendLine();
            while (xml.IndexOf("</Waypoints>") > 0)
            {
                xml = xml.Remove(0, xml.IndexOf("<Name>") + 6);
                name = xml.Substring(0, xml.IndexOf("</Name>"));
                xml = xml.Remove(0, xml.IndexOf("<Latitude>") + 10);
                double latitude = double.Parse(xml.Substring(0, xml.IndexOf("</Latitude>")), CultureInfo.InvariantCulture) * 180 / Math.PI;
                xml = xml.Remove(0, xml.IndexOf("<Longitude>") + 11);
                double longitude = double.Parse(xml.Substring(0, xml.IndexOf("</Longitude>")), CultureInfo.InvariantCulture) * 180 / Math.PI;
                xml = xml.Remove(0, xml.IndexOf("</Waypoints>") + 12);
                output.AppendLine("<waypoint name=\"" + name + "\" ignoreLand=\"false\" routeTemplatePointType=\"Fixed\" tracktype=\"RhumbLine\" useSpeed=\"false\" speed=\"0\" delay=\"PT0S\" lon=\"" + longitude.ToString(CultureInfo.InvariantCulture) + "\" lat=\"" + latitude.ToString(CultureInfo.InvariantCulture) + "\"/>");
                output.AppendLine();
            }
            output.AppendLine("</waypoints>");
            output.AppendLine();
            output.AppendLine("</template>");
            output.AppendLine();
            output.AppendLine("</sposroutetemplate>");
            try
            {
                using (StreamWriter sw = new StreamWriter(textBoxDest.Text))
                    sw.Write(output.ToString());
                MessageBox.Show("erledigt!");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Fehler beim Schreiben der Datei:\n\n" + ex);
            }
            
        }
    }
}


Dateianhang:
zip XMLConverter.zip (5,04 KB, 491 mal heruntergeladen)
Karlito

Hi,

Zitat:
Original von hornblower
Die - vor den Waypoints etc stammen aus der xml Datei und lassen dort die Unterinformationen verschwinden. siehe Bild


Nope, es ist so wie ich es mir gedacht habe großes Grinsen Die - sind nicht in der original-XML-Datei enthalten. Das ist nur die Ansicht im Browser...

Zitat:
Original von hornblower
Die Koordinaten Datei habe ich runtergeladen, jedoch kommt direkt nach dem Öffnen eine Fehlermeldung und ich muss es wieder schliessen. Keine Ahnung ob die Signatur hilft...:

Problemsignatur:
Problemereignisname: CLR20r3
Problemsignatur 01: Koordinaten.exe
Problemsignatur 02: 1.0.5557.24043
Problemsignatur 03: 550c10c6
Problemsignatur 04: mscorlib
Problemsignatur 05: 4.0.30319.34209
Problemsignatur 06: 534894cc
Problemsignatur 07: 4527
Problemsignatur 08: 105
Problemsignatur 09: System.IO.DirectoryNotFound
Betriebsystemversion: 6.3.9600.2.0.0.768.101
Gebietsschema-ID: 1031
Zusatzinformation 1: 5861
Zusatzinformation 2: 5861822e1919d7c014bbb064c64908b2
Zusatzinformation 3: a10f
Zusatzinformation 4: a10ff7d2bb2516fdc753f9c34fc3b069



@euler: In der exe die Du gebaut hast, ist der Pfad zur Eingabedatei leider noch hart kodiert (c:\home\eulerschezahl...)

Gruß,

Karlito
Es sind weitere Beiträge zu diesem Thema vorhanden. Klicken Sie hier, um sich alle Beiträge anzusehen.