For example, I want a list of banners, all will have a name, but not all say a url can I write this -
<list>
-
<banner>
-
<name>a.jpg<name>
-
</banner>
-
<banner>
-
<name>a.jpg<name>
-
<url>http://yahoo.com</url>
-
</banner>
-
</list>
-
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
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)
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)
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().
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)
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.
following the original problem I need to check wheather a parent node has a child node <data> (some will and some will not) trying -
if (typeof(x[i].getElementsByTagName("data")[0].childNodes[0].nodeValue)== "undefined")
-
data='none';
-
else
-
data=x[i].getElementsByTagName("data")[0].childNodes[0].nodeValue;
-
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?
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)
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?
Dormilich 8,658
Recognized Expert Moderator Expert
invalid XML doesn’t load.
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 ? -
function loadXMLDoc(dname)
-
{
-
if (window.XMLHttpRequest)
-
{
-
xhttp=new XMLHttpRequest();
-
}
-
else
-
{
-
xhttp=new ActiveXObject("Microsoft.XMLHTTP");
-
}
-
xhttp.open("GET",dname,false);
-
xhttp.send();
-
return xhttp.responseXML;
-
}
-
-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
<html lang="en-US" xml:lang="en-US" xmlns="http://www.w3.org/1999/xhtml">
-
<head>
-
<title>DOM Tutorial - Validate XML</title>
-
</head>
-
<body>
-
-
<script language="JavaScript" type="text/javascript">
-
document.write("<h2>Result of Validating: http://www.w3schools.com/dom/cd_catalog.xml</h2>");
-
// code for IE
-
if (window.ActiveXObject)
-
{
-
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
-
xmlDoc.async="false";
-
xmlDoc.validateOnParse="true";
-
xmlDoc.load("http://www.w3schools.com/dom/cd_catalog.xml");
-
-
if (xmlDoc.parseError.errorCode == 0)
-
{
-
document.write("The file <b>http://www.w3schools.com/dom/cd_catalog.xml</b><br />Validated Successfully without Errors");
-
}
-
else
-
{
-
document.write("<code>");
-
document.write("Error in line " + xmlDoc.parseError.line + " position " + xmlDoc.parseError.linePos);
-
document.write("<br /><br />");
-
-
document.write("Error Code: " + xmlDoc.parseError.errorCode);
-
document.write("<br /><br />");
-
-
document.write("Error Reason: " + xmlDoc.parseError.reason);
-
document.write("</code>");
-
-
document.write("<xmp>");
-
document.write("Error Line: " + xmlDoc.parseError.srcText);
-
document.write("</xmp>");
-
}
-
}
-
// code for Mozilla, Firefox, Opera, etc.
-
else if (document.implementation && document.implementation.createDocument)
-
{
-
xmlDoc=document.implementation.createDocument("","",null);
-
xmlDoc.async=false;
-
xmlDoc.load("http://www.w3schools.com/dom/cd_catalog.xml");
-
-
if (xmlDoc.documentElement.nodeName=="parsererror")
-
{
-
document.write(xmlDoc.documentElement.childNodes[0].nodeValue);
-
}
-
else
-
{
-
document.write("XML is valid");
-
}
-
}
-
else
-
{
-
document.write('Your browser cannot handle this script');
-
}
-
</script>
-
-
</body>
-
</html>
-
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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:
...
|
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
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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>...
|
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,...
| |
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |