Jump to content
Sign in to follow this  
RoxPro

A little proble with the name of the elements

Recommended Posts

Hi everyone, I'm new in XML. I have a little problem here, because I must write a XML file for a number of conf files. And then I'll parse it by expat. Now I have a problem, the XML file that I want to write in this way:

<?xml version="1.0" encoding="ISO-8859-1"?><note xmlns="RoxPro"xmlns:xsi="C:/Users/RoxPro/My Documents/"xsi:schemaLocation="RoxPro conf.xsd"><conf><network>  <config interface loopback>		   <option>		 <ifname>lo</ifname>		 <proto>static</proto>		 <ipaddr>127.0.0.1</ipaddr>		 <netmask>255.0.0.0</netmask>	   </option>  </config interface loopback>  <config interface lan>	 	   <option>		 <ifname>eth0</ifname>		 <type>bridge</type>		 <proto>static</proto>		 <ipaddr>192.168.1.1</ipaddr>		 <netmask>255.255.255.0</netmask>	   </option>  </config interface lan>  <config interface wifi>		   <option>		 <proto>static</proto>		 <ipaddr>192.168.2.1</ipaddr>		 <netmask>255.255.255.0</netmask>	   </option>  </config interface wifi></network><wireless>  <config wifi-device radio0>		  <option>		<type>atheros</type>		<channel>auto</channel>		<macaddr>00:15:6d:fc:71:ac</macaddr>		<disable>0</disable>	  </option>  </config wifi-device radio0>  <config wifi-iface>	<option>	  <device>radio0</device>	  <network>wifi</network>	  <mode>ap</mode>	  <ssid>OpenWrt</ssid>	  <encryption>none</encryption>	</option>  </config wifi-iface></wireless></conf></note>

And the conf file will be parsed like this:

config interface loopback		option ifname   lo		option proto	static		option ipaddr   127.0.0.1		option netmask  255.0.0.0config interface lan		option ifname   eth0		option type	 bridge		option proto	static		option ipaddr   192.168.1.1		option netmask  255.255.255.0config interface wifi		option proto	static		option ipaddr   192.168.2.1		option netmask  255.255.255.0

But here's a problem, I can't make an element with spaces betweens the words, I used the program who names XMLwriter, it says "Missing equals sign between attribute and attribute value" with this tag <config interface loopback>, so can someone helps me how to make an well formed element and not so complicate to parse, thanks.

Share this post


Link to post
Share on other sites

Element names must be one word. It sounds like you are using the extra words as data or meta data, so they shouldn't be part of the element name anyway. I suggest one of these patterns, depending on your requirements:

<config interface="loopback">or<config>   <interface>loopback</interface>

Share this post


Link to post
Share on other sites
Element names must be one word. It sounds like you are using the extra words as data or meta data, so they shouldn't be part of the element name anyway. I suggest one of these patterns, depending on your requirements:
<config interface="loopback">or<config>   <interface>loopback</interface>

Thanks for your help, but I want to ask, if I do like what you write, it will not be easy to parse the XML file and create the file "network", because I'm using expat now, and I have already problems to parsing and creating files with it, i don't know if I do like that, it will be more complicate or not...

Share this post


Link to post
Share on other sites

Complicated is better than broken. :)Part of the difficulty might be that you are using information inconsistently, so trying to create a consistent structure will be difficult. These 3 tags show what I mean:

1. <config interface loopback>2. <config wifi-device radio0>3. <config wifi-iface>

All the tags that contain the word "interface" (like #1) can be reorganized the way I showed you in my first post:

<config interface="loopback">or<config>   <interface>loopback</interface>

But 2 and 3 don't have the word "interface", so that won't work for them. Tag 2 could be reorganized like this:

<config device="radio0">or<config>   <device>radio0</device>

But that will not work for tag 3. You see the problem? The data is not consistent. It would be more consistent and easier to parse if ALL config elements had an interface attribute or child, not just some of them. Fixing this will mean planning part of your structure again.If you cannot restructure the config elements so they are organized the same, the best I can suggest might look like this:

<config>   <data>interface loopback</data>

The idea is that all the extra words in your config tags can go inside a child element that is used in EVERY config element. It doesn't have to be called "data" -- use your own name if you like.XML is the most useful, and easiest to parse, when the structure is predictable.

Edited by Deirdre's Dad

Share this post


Link to post
Share on other sites
Complicated is better than broken. :)Part of the difficulty might be that you are using information inconsistently, so trying to create a consistent structure will be difficult. These 3 tags show what I mean:
1. <config interface loopback>2. <config wifi-device radio0>3. <config wifi-iface>

All the tags that contain the word "interface" (like #1) can be reorganized the way I showed you in my first post:

<config interface="loopback">or<config>   <interface>loopback</interface>

But 2 and 3 don't have the word "interface", so that won't work for them. Tag 2 could be reorganized like this:

<config device="radio0">or<config>   <device>radio0</device>

But that will not work for tag 3. You see the problem? The data is not consistent. It would be more consistent and easier to parse if ALL config elements had an'tn interface attribute or child, not just some of them. Fixing this will mean planning part of your structure again.?If you cannot restructure the config elements so they are organized the same, the best I can suggest might look like this:

<config>   <data>interface loopback</data>

The idea is that all the extra words in your config tags can go inside a child element that is used in EVERY config element. It doesn't have to be called "data" -- use your own name if you like.XML is the most useful, and easiest to parse, when the structure is predictable.

:) Thanks a lot for your explication! Thanks! OK, I tried your structure yesterday, I have another problem... :P I don't know if you know the TR-069, on what I'm working, the conf file isn't simply made by xml file, I have to create a XSD file to define it... The problem is, these three tags name config all of them, so I can't define three different tags with same name config, that is where I have a really difficult. I tried use _(ex: config_interface_loopback), but with expat, I have no idea how can I delete them when I parse them and create new files... So what's your suggestion Deirdre's Dad? Is there any solution feasible? Thanks!

Share this post


Link to post
Share on other sites

What language are you working with? Expat has various language wrappers. Do you have to use Expat to begin with?When you parse an element, you should also be able to get its attributes in a collection. You can distinguish between different config elements by the presense and/or value of certain attributes. You should also be able to declare variables that would indicate the context you're in, so that if, for example, a config element within a config element has different semantics, you could react accordingly.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...