We’re developing an socketserver who should receive xml.
our specification said, a xml stream starts with
<?xml version="1.0" encoding="UTF-8"?><logmsg>
and end with
</logmsg>
Now, i try to add the XML-declaration line to my XML-Object, without success. A own written socket monitor shows only the <logmsg></logmsg>-Nodes. Also trace show similiar informations.
Is it possible to add the xml-declaration to an XML-Object overall?
logInfo = new XML('<?xml version="1.0" encoding="UTF-8"?><logmsg></logmsg>');
logInfo.@severity = value.serverity;
logInfo.@version = value.version;
traces
<logmsg severity="2" version="0.1">
<timestamp offset="120">
1318939755
</timestamp>
</logmsg>
and
var doc:XMLDocument = new XMLDocument('<logmsg></logmsg>');
doc.docTypeDecl ="<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
logInfo = new XML(doc);
has the same result, not exactly of course, but without the declaration line.
Can you help me?
First the short answer: there is no way that I know of to get XML printed with prolog in AS3 (what you call declaration is, according to the specs, a prolog).
More elaborate answer: XML class is responsible for the content of the XML document. Prolog is not a part of the document. XML classifies node as processing instruction, but this particular instruction is reserved and is not printable (this is a requirement of XML format that it will not appear inside the content). Other processing instructions, by default, aren’t added to the document at the time of parsing it, but you could add them by using
XML.ignoreProcessingInstructions = false.Here you would need to be a bit more open-minded then programmers usually are, I don’t write this to bash random people 🙂 so, try to find some rationale for you, if you will. XML is the worst known yet very popular format for data storage and transfer. It was designed for presenting the data, but it is a bad tool for data exchange or storage. XML is extremely verbose and limited in a sense of type information and kinds of relations it can represent. On the other hand, it is too complex (think of DTD, namespaces and all such thing) for the humble results it can achieve. The good alternatives, especially for Flash exist, AMF3 is in all aspects superior to XML. There also exists Ptotocol Buffers implementation for AS3. So if you wanted to use an established and well-designed format, those are the options. If working off the local storage, SQLite may be bundled with AIR programs, which is a good option too.
Few hints regarding your code:
is a bad way to create instances of XML class. XMLs are same sort of objects as strings are. There is no justification for using
newoperator with XML.newoperator is supported only so you could abstract object creation, but is not needed, when the class is known at compile time. Upon parsing, AS3 parser will eliminate redundant empty text nodes, so the code equivalent to the code above, but shorter and easier for the parser is:XMLDocument class – this, if you insist on using XML may provide an answer. This class is in general simpler and more familiar to programmers who used the DOM API before. It also has less pitfalls in that you should know what E4X expressions compile to in order to write good performing code. XMLDocument is marginally faster for simple operations like attribute access or child node access. It is however more verbose. You can extend XMLDocument and make it print in any way you like, so printing the prolog would be a trivial task, if not yet implemented.