 |
| XML General XML discussions. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the XML section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

June 6th, 2008, 01:18 AM
|
|
Authorized User
|
|
Join Date: Jun 2008
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Edit xml
Hello I'm new in XML
I would like to edit my xml file using ASP or java script..
I mean like this...When I click submit button to update, then it is proccesed to update it...
I've tried to look for but most of them use XSL...
<form>
<description>
<desc_id>024/HRCS-SP/P/7/98</desc_id>
<title>Kebijakan1</title>
<dates>25/5/2007</dates>
<author>Smart</author>
<file_name>Policy1</file_name>
<type>pdf</type>
<category>surat_keputusan</category>
<sub_cat1>tidak_berlaku</sub_cat1>
<sub_cat2>1981</sub_cat2>
</description>
</form>
My xml file named description2.xml
Need help...Urgent
Thx
|
|

June 6th, 2008, 02:31 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
There's a good reason why most examples show this being done using XSLT - it's a better solution in most cases than writing low-level procedural code.
But you can do it in Java or C# or Javascript or any other language that supports a DOM interface. In general, you invoke the XML parser to create a DOM tree in memory, you then navigate that tree to find the nodes you need to modify, make the modifications that are needed, and then invoke the DOM serializer to write the results back to filestore. The code can be tedious and longwinded, but it is not difficult.
You'll find examples of DOM coding in most XML textbooks. They may use Java rather than Javascript, but it's not that different.
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
|

June 6th, 2008, 02:51 AM
|
|
Authorized User
|
|
Join Date: Jun 2008
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I've tried to use java script and I prefer to use it rather than XSL...
But how it could not work? And when I want to print inside looping, the result could not appear. Here is the java script that I used.
<script type="text/javascript">
var xmlDoc=null;
if (window.ActiveXObject)
{
// code for IE
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
}
else if (document.implementation.createDocument)
{
// code for Firefox, Mozilla, Opera, etc.
xmlDoc=document.implementation.createDocument(""," ",null);
}
else
{
alert('Your browser cannot handle this script');
}
//var test = "<%= oldidname%>";
//var path = "form/description[desc_id=\""+ test +"\"]//*";
//document.write(test);
if (xmlDoc!=null)
{
xmlDoc.async=false;
xmlDoc.load("description2.xml");
var test = "<%= oldidname%>";
path = "form/description[desc_id=\""+ test +"\"]//*";
var nodes=xmlDoc.selectNodes(path);
document.write(test);
document.write("<br />");
document.write(path);
for (i=0;i<nodes.length;i++)
{
//document.write(nodes[i].childNodes[0].nodeValue);
//document.write(path);
//x=xmlDoc.getElementsByTagName("nodes[i].childNodes[0].nodeValue");
//x=xmlDoc.getElementsByTagName("nodes[i]");
//x.nodeValue="nodes[i].childNodes[0].nodeValue";
//document.write(x.nodeValue);
//document.write(x);
}
}
</script>
|
|

June 6th, 2008, 03:02 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
Well, it looks as if your XPath expression hasn't selected any nodes. Since we haven't seen your source document, there could be any number of reasons. One of the most common mistakes is to overlook that the elements are in a namespace. Another reason might be that you have supplied the parameter value oldidname incorrectly.
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
|

June 6th, 2008, 03:08 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
You need to show the actual value the XPath including the actual oldidname. From what you've shown it should work.
--
Joe ( Microsoft MVP - XML)
|
|

June 6th, 2008, 03:44 AM
|
|
Authorized User
|
|
Join Date: Jun 2008
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
oldidname is variable that I got from asp code...
Actually there are still some ASP codes above the Javascript code that I show to you. So, I made 2 part in 1 file named description_update.asp
Here are inside that file:
1. in ASP code (to update the new record to database) and it has already successful...
2. in Java Script (to update the new record to xml file). This is the code that I show you...
I used "oldidname" because that is a variable name that is used to decide which id that I wanna update....
When I run this file, it just update to the database and print the result before looping... When I try to print inside the looping, it does not show anything.
|
|

June 6th, 2008, 05:54 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
Yes, I know where oldidname comes from but what does it contain, what is the XPath exactly?
--
Joe ( Microsoft MVP - XML)
|
|

June 6th, 2008, 06:57 AM
|
|
Authorized User
|
|
Join Date: Jun 2008
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
(ASP)
...
if (xmlDoc!=null)
{
xmlDoc.async=false;
xmlDoc.load("description2.xml");
var test = "<%= oldidname%>";
path = "form/description[desc_id=\""+ test +"\"]//*";
var nodes=xmlDoc.selectNodes(path);
document.write(test);
document.write("<br />");
document.write(path);
for (i=0;i<nodes.length;i++)
{
//document.write(nodes[i].childNodes[0].nodeValue);
//document.write(path);
//x=xmlDoc.getElementsByTagName("nodes[i].childNodes[0].nodeValue");
//x=xmlDoc.getElementsByTagName("nodes[i]");
//x.nodeValue="nodes[i].childNodes[0].nodeValue";
//document.write(x.nodeValue);
//document.write(x);
}
}
I used variable called "path" (in blue color) as an xpath...
So, when I click "update" and I submit the updated data, only the data that has certain "id" which is updated...
So, when I run that javascript, it just print out like this:
12345
form/description[desc_id="12345"]//*
"12345" is the oldidname but, it will be printout different, depends on which data that we were clicked to be updated.
I have ho idea what should I put inside looping...
And, when I try to uncommented them, it still does not show anything...just show oldidname
I hope someone can help me=(
Thx
|
|

June 6th, 2008, 07:03 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
Well, for a start, what is the value of nodes.length?
Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
|
|

June 6th, 2008, 07:11 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
Well I'd change the XPath to:
Code:
path = "form/description[desc_id=\""+ test +"\"]/*";
Then your loop should be:
Code:
for (i=0;i<nodes.length;i++)
{
document.write(nodes[i].nodeName);
document.write(nodes[i].childNodes[0].nodeValue);
}
You didn't actually describe what you wanted to see.
Also you should not use Microsoft.XMLDOM but msxml2.domdocment3.0 and use
Code:
setProperty("SelectionLanguage", "XPath")
or better still one of the tried and tested libraries such as zXml that handle cross-browser differences when using XML.
--
Joe ( Microsoft MVP - XML)
|
|
 |