Create xml dom using java.

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Create xml dom using java.

Reece-6
Hello everyone!

I am attempting to create a level for enigma using the built in xml dom
package packaged with java.
I am having trouble doing this because of the "el:" prefix that needs
added to all xml tags in the xml portion of the level.
Does anyone have any knowledge of the java xml parser?
If anyone does, could you help me figure this out? It will be much
appreciated :)

Thanks, Reece

_______________________________________________
Enigma-devel mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/enigma-devel
Reply | Threaded
Open this post in threaded view
|

Re: Create xml dom using java.

Erich Schubert
Hello Reece,
You need to define the prefixes using the Namespace APIs.
Usually, they do not need to be "el:". It's up to the user to choose these prefixes.
The key part is to map the prefix to a namespace, there should be a xmlns:el attribute on an appropriate parent node.

Typical code looks (here: SVG, adding an xlink: namespace) like this:

root = document.getDocumentElement();
root.setAttribute(SVGConstants.XMLNS_PREFIX, SVGConstants.SVG_NAMESPACE_URI);
root.setAttributeNS(SVGConstants.XMLNS_NAMESPACE_URI, SVGConstants.XMLNS_PREFIX + ":" + SVGConstants.XLINK_PREFIX, SVGConstants.XLINK_NAMESPACE_URI);

using a namespaced attribute:
elem.setAttributeNS(SVGConstants.XLINK_NAMESPACE_URI, SVGConstants.XLINK_HREF_QNAME, value);

creating a namespaced element:
document.createElementNS(namespaceURI, qualifiedName);

qualifiedName here is prefix+":"+tag name
namespaceURI is the actual URI of the namespace. I guess the XML DOM factory will check the the namespace has been defined as above.

Regards,
Erich

_______________________________________________
Enigma-devel mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/enigma-devel
Reply | Threaded
Open this post in threaded view
|

Re: Create xml dom using java.

Reece-6
Thanks Erick,

However, I wasn't able to figure out how to implement the code.
Would you like to take a look at the code?
I can send you the file or the code is also available on Git via a
SourceForge project.
http://sourceforge.net/projects/enigmaeditor/ (The projects SourceForge
page.)
http://sourceforge.net/p/enigmaeditor/sourcecode/ci/cb11dd3eed66e2ecfed8d59892282d7991cedb30/tree/NetBeans%20Project/src/egle/MyFileOperations.java#l114 
(This is the class file that I'm talking about)
The method "exportXML()" contains the code I have so far.

I also have another problem that you might be able to help me with, when
I use transformer factory/transformer to output my dom tree to an xml
file, the transformer outputs into a single line.
Do you know how to get the transformer to output the xml file with line
breaks and indentations(tabs)?



On 6/19/2012 1:38 AM, Erich Schubert wrote:

> Hello Reece,
> You need to define the prefixes using the Namespace APIs.
> Usually, they do not need to be "el:". It's up to the user to choose
> these prefixes.
> The key part is to map the prefix to a namespace, there should be a
> xmlns:el attribute on an appropriate parent node.
>
> Typical code looks (here: SVG, adding an xlink: namespace) like this:
>
> root = document.getDocumentElement();
> root.setAttribute(SVGConstants.XMLNS_PREFIX,
> SVGConstants.SVG_NAMESPACE_URI);
> root.setAttributeNS(SVGConstants.XMLNS_NAMESPACE_URI,
> SVGConstants.XMLNS_PREFIX + ":" + SVGConstants.XLINK_PREFIX,
> SVGConstants.XLINK_NAMESPACE_URI);
>
> using a namespaced attribute:
> elem.setAttributeNS(SVGConstants.XLINK_NAMESPACE_URI,
> SVGConstants.XLINK_HREF_QNAME, value);
>
> creating a namespaced element:
> document.createElementNS(namespaceURI, qualifiedName);
>
> qualifiedName here is prefix+":"+tag name
> namespaceURI is the actual URI of the namespace. I guess the XML DOM
> factory will check the the namespace has been defined as above.
>
> Regards,
> Erich

_______________________________________________
Enigma-devel mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/enigma-devel
Reply | Threaded
Open this post in threaded view
|

Re: Create xml dom using java.

Erich Schubert
Hello Reece,
use setAttributeNS, not setAttribute ("without namespace") whenever you need a namespaced attribute (with a colon in the name).

The transformer should have a hint to set pretty printing.

best regards,
Erich Schubert

_______________________________________________
Enigma-devel mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/enigma-devel