473,503 Members | 1,696 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Must all parent node have the same types of childs?

Mike Kypriotis
37 New Member
For example, I want a list of banners, all will have a name, but not all say a url can I write this
Expand|Select|Wrap|Line Numbers
  1. <list>
  2. <banner>
  3. <name>a.jpg<name>
  4. </banner>
  5. <banner>
  6. <name>a.jpg<name>
  7. <url>http://yahoo.com</url>
  8. </banner>
  9. </list>
  10.  
is this valid?
P.S. I do not want to include an empty <url> child to all <banner> since that would be a waste and I do not want to rename the <banner> with <url> to some thing else
Apr 1 '11 #1
10 2380
Dormilich
8,658 Recognized Expert Moderator Expert
it is valid in the sense as it follows the XML base rules (given that you use UTF-8).

for stricter validaty, you’d need a DTD or Schema (XSD or RelaxNG)
Apr 1 '11 #2
Mike Kypriotis
37 New Member
aside from the DTD/UTF-8 because everything I have seen up to now sticks with the same childs in every parent node with the same name, is there some downside/disadvantange with this logic?
(When extracting data in my for loop I plan to use a simple if to see wheather the <url> child exists or not in each parent)
Apr 1 '11 #3
Dormilich
8,658 Recognized Expert Moderator Expert
I think it’s a matter of personal preference. if you want only the URLs, use XPath or getElementsByTagName().
Apr 1 '11 #4
Mike Kypriotis
37 New Member
All the job will be done with JS. Know any good tutorial?
(Also for reading an xml file, the only option is AJAX? Because the point of the whole thing is to make an application which works offline on the browser-thus the javascript)
Apr 2 '11 #5
Dormilich
8,658 Recognized Expert Moderator Expert
without help of a server side script, AJAX is the only option in mind for reading XML in a HTML page.
Apr 3 '11 #6
Mike Kypriotis
37 New Member
following the original problem I need to check wheather a parent node has a child node <data> (some will and some will not) trying
Expand|Select|Wrap|Line Numbers
  1. if (typeof(x[i].getElementsByTagName("data")[0].childNodes[0].nodeValue)== "undefined")  
  2.   data='none';
  3.   else
  4.   data=x[i].getElementsByTagName("data")[0].childNodes[0].nodeValue; 
  5.  
where x[i] the current parent element within the loop but still get an error that x[i].getElementsByTagName("data")[0].childNodes[0].nodeValue is undefined, note that if the parent has this node everything is works ok, so x[i].getElementsByTagName("data")[0].childNodes[0].nodeValue gets the value if it finds it, any ideas?
Apr 4 '11 #7
Dormilich
8,658 Recognized Expert Moderator Expert
check the length of the getElementsByTagName() list. childNodes[0].nodeValue is undefined if there is either no child node (empty tag) or no parent node (no parent, no child)
Apr 4 '11 #8
Mike Kypriotis
37 New Member
worked well, with this I can check the existance of data. The other think I want to do is check the overall xml file for syntax errors. Because a admin will upload it, say he deleted a '<' when changing and now a node is not closed properly do you now any way to check it?
Apr 5 '11 #9
Dormilich
8,658 Recognized Expert Moderator Expert
invalid XML doesn’t load.
Apr 5 '11 #10
Mike Kypriotis
37 New Member
found this from w3schools and seems to work, will dig to see how it is done.
P.S. You load the xml with the way shown below or with AJAX like any other data like this ?
Expand|Select|Wrap|Line Numbers
  1. function loadXMLDoc(dname)
  2. {
  3. if (window.XMLHttpRequest)
  4.   {
  5.   xhttp=new XMLHttpRequest();
  6.   }
  7. else
  8.   {
  9.   xhttp=new ActiveXObject("Microsoft.XMLHTTP");
  10.   }
  11. xhttp.open("GET",dname,false);
  12. xhttp.send();
  13. return xhttp.responseXML;
  14.  
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html lang="en-US" xml:lang="en-US" xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <title>DOM Tutorial - Validate XML</title>
  5. </head>
  6. <body>
  7.  
  8. <script language="JavaScript" type="text/javascript">
  9. document.write("<h2>Result of Validating: http://www.w3schools.com/dom/cd_catalog.xml</h2>");
  10. // code for IE
  11. if (window.ActiveXObject)
  12. {
  13. var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
  14. xmlDoc.async="false";
  15. xmlDoc.validateOnParse="true";
  16. xmlDoc.load("http://www.w3schools.com/dom/cd_catalog.xml");
  17.  
  18. if (xmlDoc.parseError.errorCode == 0) 
  19.   {
  20.   document.write("The file <b>http://www.w3schools.com/dom/cd_catalog.xml</b><br />Validated Successfully without Errors");
  21.   }
  22. else
  23.   {
  24.   document.write("<code>");
  25.   document.write("Error in line " + xmlDoc.parseError.line + " position " + xmlDoc.parseError.linePos);
  26.   document.write("<br /><br />");
  27.  
  28.   document.write("Error Code: " + xmlDoc.parseError.errorCode);
  29.   document.write("<br /><br />");
  30.  
  31.   document.write("Error Reason: " + xmlDoc.parseError.reason);
  32.   document.write("</code>");
  33.  
  34.   document.write("<xmp>");
  35.   document.write("Error Line: " + xmlDoc.parseError.srcText);
  36.   document.write("</xmp>");
  37.   }
  38. }
  39. // code for Mozilla, Firefox, Opera, etc.
  40. else if (document.implementation && document.implementation.createDocument)
  41. {
  42. xmlDoc=document.implementation.createDocument("","",null);
  43. xmlDoc.async=false;
  44. xmlDoc.load("http://www.w3schools.com/dom/cd_catalog.xml");
  45.  
  46. if (xmlDoc.documentElement.nodeName=="parsererror")
  47.   {
  48.   document.write(xmlDoc.documentElement.childNodes[0].nodeValue);
  49.   }
  50. else
  51.   {
  52.   document.write("XML is valid");
  53.   }
  54. }
  55. else
  56. {
  57. document.write('Your browser cannot handle this script');
  58. }
  59. </script>
  60.  
  61. </body>
  62. </html>
  63.  
Apr 5 '11 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

2
14869
by: john smith | last post by:
Hello, I am having trouble getting the parent node of an xml document using xslt. I am trying to return the node wine, but this does not return wine but the element values under wine. I...
3
3150
by: MrNobody | last post by:
Ok, so I'm absoultely positive this was working last night, but today I'm getting this error in my program when I try to add a child node to a parent node of a tree... The error says: ...
5
3799
by: rmlakshmanan | last post by:
In javascript a tree structure node i have a subnode and it has parentnode and that parentnode has one parent how do i get all parent node when i click the subnode
14
11484
by: neerajb | last post by:
Hi, I am having an XML document(input.xml) which is showing the menu heirarchy used in my application.My requirement is to add "submenu" tag to those menuitems who are having the child menuitems...
0
1050
by: sattawar | last post by:
Hi,...I need to have a checkbox in the parent node of a treeview...where as it must not appear for child nodes. It must be possible to check or un check the checkbox. If any one knows please help...
4
6461
by: MA | last post by:
Hi, How to access the total number of child nodes from a parent node. For example, I would like to get the total number of child nodes from <parent1and <parent2node. The SelectNodes method...
3
5427
by: Irocivan | last post by:
Hello, I download a nice collapse menu for free distribution from the internet. It works very well except that none of the parents nodes are clickable (i.e. when clicking on the parent node, the...
0
972
by: Limno | last post by:
Hi, I have a treeview control with showcheckboxes = All in my C#.net web application. When i check on the child node its parent node should be checked automatically.similarlly when i uncheck...
4
3572
by: jmDesktop | last post by:
I have searched everywhere and tried several things. I have a treeview with and want to be able to only select a parent node. For example: root //don't want to drag this -parent1 //yes, drag...
1
3656
by: SWETA123 | last post by:
I will very helpful to you if you can help me out in my below problem. I am not getting idea what should be the right approach for this. Here is my problem. I have below (sample)xml. <Rules>...
0
7201
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7083
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7328
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6988
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5011
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3166
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
734
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
379
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.