Jump to content

Comparing two xml files


exelio
 Share

Recommended Posts

hi all,I want to compare 2 xml files with each other. One xml file is standard on my pc and has the name standard.xml. the other xml file is on the server and has the name server.xmli want to compare these 2 files with each other and if it contains new info it should be added in the pc.xml file.in msdn, i found that XML diff and patch tool is used to compare two xml files. but i do not want to add a additional tool to be installed for this purpose.i tried to do the comparison by getting the nodes in a XmlNodeList and comparing the two nodelists. but i am struck at one point.how can i find out whether a node already exists in a xml document. say for eg,in my xml file,i have a structure like this,<Main><url type_node="S" tool_tip="http://www.mysite.com/sample.xml">http://www.mysite.com/sample.xml</url><Category2><url type_node="S" tool_tip="http://www.mysite.com/sample1.xml">http://www.mysite.com/sample1.xml</url></Category2></Main>in this,Category2 is a childnode of Main. i want to check if the url element for sample exists already in the document.how can i do this. i tried using xmldoc.getElementsByTagName(node.innerText);but i am always getting a count of 1 even if the node does not exits in the document.Hope i am clear in my question.Any suggestions?Thanks in advance.RegardsExelio

Link to comment
Share on other sites

What language is this in? JavaScript or ASP(.NET)? Is the file always stored remotely, or is it stored on the same server?Either way, instead of always (down)loading the file, you should instead check if it was modified and do whatever you do when it is. This will ensure that even if the old stuff is edited (rather than a new node being added), you'll still be up to date.If using JavaScript, you can perform XMLHttpRequest() to the XML file with the HEAD method instead of the GET one, and check the value of the "Last-Modified" header. If it's less than the value of the last modification time of your own document, then udate.It's pretty much the same deal with ASP(.NET), but the actual method for that is different and I don't know it. Look somewhere with the file system methods.

Link to comment
Share on other sites

hi,Thanks for the response. i am using C# in a windows based application. thanks for your suggestion. i am creating a xml file based on the number of folders in the server. a new folder maybe added or a folder might be deleted or renamed. as you suggested, i will try using the FileInfo to get the information about last modified. and then try updating the xml file.but still, if at all a new folder has been added i need to create a node in my xmlfile along with the information about the folders or files inside. for that i need to check if the folder(say new category name) already exists in the xmlfile.How can do this?i know we can traverse through the entire xml file and find out. but is there a faster way to traverse all the nodes in the xmlfile so as to find out whether the node exists?Thanks once againRegardsexelio

Link to comment
Share on other sites

I'd say plain DOM is a very fast way of doing this, maybe even the fastest... if you can swallow the bigger RAM consumption.If you can't swallow it, then find yourself a streaming (SAX) processor. Such processors read the XML from top to bottom, scanning nodes as they go and allowing you to check up info. This consumes far less memory, but means you have to manually check each node if its name/value/whatever is what you want, and stop when you find it.For C#, and the .NET framework, MS seem to have the xmlTextReader class providing just that functionality.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...