NextPVR Forums
  • ______
  • Home
  • New Posts
  • Wiki
  • Members
  • Help
  • Search
  • Register
  • Login
  • Home
  • Wiki
  • Members
  • Help
  • Search
NextPVR Forums Public Developers v
« Previous 1 … 83 84 85 86 87 … 93 Next »
need help with xml

 
  • 0 Vote(s) - 0 Average
need help with xml
reven
Offline

Posting Freak

Posts: 5,782
Threads: 396
Joined: Sep 2004
#1
2004-11-25, 07:11 PM
im writing a new plugin which im trying to make it remember the directories last view (list, icon etc). ive decided to do it in an xml file like
<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">
<dirviews>
 <D>
   <My_Videos>1</MY_VIDEOS>
   <Recordings>0</Recordings>
 </D>
</dirviews>
[/QUOTE]
so D:\MyVideos has the view associated with 1 and D:\Recordings has the view associated with 0.  im having problems adding things that arent in the xmlfile already thou.
im using this code (from a plugin config i was using as blueprint)
<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">
XmlDocument newDocument = new XmlDocument();
newDocument.LoadXml(newNodeXML);

XmlNode imported = DirectoryView.ImportNode(newDocument.DocumentElement.LastChild, true);

DirectoryView.DocumentElement.AppendChild(imported);

node = DirectoryView.SelectSingleNode(DirToNodeString(currentDirectory.Location));
[/QUOTE]
newNodeXml = a string for the new node eg &quot;/dirviews/d/recordings&quot;
DirectoryView is my xmldocument im using

but that code gives me xml file like
<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">
<dirviews>
 <D>
   <My_Videos>1</MY_VIDEOS>
 </D>
 <D>
   <Recordings>0</Recordings>
 </D>
</dirviews>
[/QUOTE]

i figure the problem is in &quot;AppendChild&quot; and/or &quot;LastChild&quot;, but i cant figure out how to fix this problem, if anyone knows what im doing wrong and how i can fix this, your help would be very much appreciated;
cheers in advance.

oh and if your interested heres info about the plugin and its current status.
sub
Offline

Administrator

NextPVR HQ, New Zealand
Posts: 106,807
Threads: 769
Joined: Nov 2003
#2
2004-11-25, 07:30 PM
You probably need to a SelectSingleNode to get the /dirviews/D, which is the parent, then call AppendChild on that.

[b Wrote:Quote[/b] ]oh and if your interested heres info about the plugin and its current status.
This just give a message about being a restricted area
KingArgyle
Offline

Posting Freak

Posts: 1,271
Threads: 95
Joined: Nov 2004
#3
2004-11-25, 10:33 PM
XmlDocument newDocument = new XmlDocument();
newDocument.LoadXml(newNodeXML);

Well, going from strictly and XML DOM model, what your best bet is to use an XPath statement to get you the node you want:

Something like /dirviews/D

XmlNode node = DirectoryView.SelectSingleNode(&quot;/dirviews/D&quotWink;

That should return you a Node that contains the My Recordings element.

Next you'll want to create the new Element, something like

IXMLDOMElement recordings = DirectoryView.createElement(&quot;Recordings&quotWink;

recordings.Text = &quot;0&quot;;

Now you want to append the Node to the one that you had selected.

node.AppendChild(recordings);

That should append to your XML DOM, the Recordings element to the D node.

The key is to realize that when you are using SelectSingleNode is just giving you a pointer to a node. If you want to add to that node, then you need to Append to that node, and not the Dom document it self.

XPath can be frustrating to figure out at times, but once you do, SelectNodes and SelectSingleNode will be your best friends for manipulating XML through code.

The following site has a good simple example of working with the XML Dom.

http://www.freevbcode.com/ShowCode.asp?ID=1919
reven
Offline

Posting Freak

Posts: 5,782
Threads: 396
Joined: Sep 2004
#4
2004-11-26, 02:30 AM
ok cheers for your help, ill look into it later on.
i fixed the url to the plugins info, the above link is working now, and heres another one.
jasonf
Offline

Member

Posts: 121
Threads: 7
Joined: Oct 2004
#5
2004-11-26, 02:32 AM
Just an added tip:  Sometimes, it might be easier to modify the XML in string form, and then reload it into the DOM Document (remember that XML is a string first, and data second, but only after parsing).  

If you're really good at classic string manipulation, but a little inexperienced with DOM, then this might be a great way to quickly accomplish your task.


And the screencaps look great!
JasonF
reven
Offline

Posting Freak

Posts: 5,782
Threads: 396
Joined: Sep 2004
#6
2004-11-26, 03:10 AM
yeah im just wondering if say the /dirview/d/ exists, but how about
/dirview/d/folder/folder/folder/folder/folder/folder or something. since most of the virtual root elements will be directory embedded (eg D:\My Videos\Tv Shows\Wink. i thought there might be an easy xml way to just add a node which is placed within the appropriate tags first and creates ones that arent there. say if D\D1\D2\D3\D4
if D\D1\D2 is there it inserts D3\D4 in there. once the virutal root is setup the rest shouldnt be a problem since they are only one more directory in.

oh i just thought of perhaps another problem say if
<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">
<dirview>
<d>0</d>
<d>
<My_Videos>0</My_Videos>
</d>
</dirview>
[/QUOTE]
i suppose i could make a new one called ROOT for each folder (might just cause some problems if someone has a folder called ROOT. in that case i could use DFFDASFDDSFDSAF (random letters i hit [Image: smile.gif])
or could i use something like &lt;D VIEW=&quot;0&quot;&gt; but in that case every folder tag would have to be like that.

any thoughts recommendations, i knew i should have paid more attention to the xml lectures (they were just so boring Tongue)
KingArgyle
Offline

Posting Freak

Posts: 1,271
Threads: 95
Joined: Nov 2004
#7
2004-11-26, 04:32 AM
Well, if you know what the path is, then use an XPATH to SelectSingleNode and then append your new path to that.

node = SelectSingleNode(&quot;/dirview/d/d1/d2&quotWink;

Create a new element:

Element addNode = XMLDom.createElement(&quot;D3&quotWink;
Element addNode2 = xmlDom.createElement(&quot;D4&quotWink;

To append to the node:

node.AppendChild(addNode.AppendChild(addNode2));

You should now have:

/dirview/d/d1/d2/d3/d4

The best bet would probably be to put this in a private method, and then call it when you need to add a node as you are navigating and building your tree.

If you have a seperate XML file that you are trying to use as input, I might suggest using XSLT and the document() function to read in the XSLT and append the nodes where you need them.

the XSLT Faq is a great resource for these types of XML manipulations.

http://www.dpawson.co.uk/xsl/xslfaq.html
reven
Offline

Posting Freak

Posts: 5,782
Threads: 396
Joined: Sep 2004
#8
2004-11-26, 05:12 AM
im loading and xml document on load and using a in memory version of it, and saving the xml document back to the original xml file when the plugin is deactivated.
thanks KingArgyle ill write a private method to cycle thru the nodes have string &quot;\d1\d2\d2\d2&quot; make that into string array with splitchar() (handy method) and just cycle thru that if an add the missing nodes, ill mostly like use &quot;ROOT&quot; for &quot;this&quot; dir (ie &quot;\d1\d2\d3\d4&quot; d2=view is d1\d2\root), ill do it tomorrow, its friday night after all [Image: smile.gif]

well i had a bit of time before going out so i gave it a shot, and well its working perfect (well still testing but im pretty sure its working perfect, havent come across a bug yet)
<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">
string nodestring = "/"+strNode[0];
node = DirectoryView.SelectSingleNode(nodestring);
for(int i=1;i<strNode.Length;i++){
if(DirectoryView.SelectSingleNode(nodestring +"/"+strNode[i]) == null){
DirectoryView.SelectSingleNode(nodestring).AppendChild(DirectoryView.CreateElement(strNode[i]));
nodestring += "/"+strNode[i];
}else{
nodestring += "/"+strNode[i];
}
}
[/QUOTE]
strNode = &quot;\DirView\Drive\Folder\Root&quot;.split(\Wink (like that)
DirectoryView is my xml document

which once saved produces something like
<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">
<DirViews>
<ROOT>
<ROOT>2</ROOT>
</ROOT>
<C>
<ROOT>0</ROOT>
<Config.Msi>
<ROOT>1</ROOT>
</Config.Msi>
</C>
<D>
<My_Videos>
<TV_Shows>
<ROOT>0</ROOT>
<Enterprise>
<ROOT>1</ROOT>
<Season_2>
<ROOT>0</ROOT>
</Season_2>
<Season_4>
<ROOT>0</ROOT>
</Season_4>
</Enterprise>
<Blackadder>
<ROOT>1</ROOT>
</Blackadder>
<Jake_2.0>
<ROOT>0</ROOT>
</Jake_2.0>
</TV_Shows>
<ROOT>1</ROOT>
</D>
</DirViews>
[/QUOTE]
the only problem is root/root but it works cant be bothered changing it (if its not broke dont fix right [Image: smile.gif])
KingArgyle
Offline

Posting Freak

Posts: 1,271
Threads: 95
Joined: Nov 2004
#9
2004-11-26, 02:30 PM
Just a suggestion, you might also consider a slight restructuring of the XML to help eleviate the directory name conflicts with the Element names. Something like this would probably help:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">
<dirviews>
<root view="2"/>
<drive name="C">
<folder name="root" view="1">
<file>Config.Msi</file>
</folder>
</drive>
<drive name="D">
<folder name="My Videos" view="1">
<folder name="TV Shows" view="0">
<folder name="Enterprise" view="1">
<folder name="Season 2" view="0"/>
<folder name="Season 4" view="0"/>
</folder>
<folder name="Blackadder" view="0"/>
<folder name="Jake 2.0" view="0"/>
</folder>
</folder>
</drive>
</dirviews>
[/QUOTE]

It also describes what the elements actual are in the XML file a bit better. From this I can tell that it is a directory structure and that it contains drives, folders, and files. I subsituted the view for the ROOT element, but that can be easily changed. The XML Dom has methods for accessing the attributes of an XML element so shouldn't be much coding change to handle this.

Also no conversion of folders with space in them has to be done before populating in the xml attirbutes unlike making them Elements.
« Next Oldest | Next Newest »

Users browsing this thread: 1 Guest(s)



  • View a Printable Version
  • Subscribe to this thread
Forum Jump:

© Designed by D&D, modified by NextPVR - Powered by MyBB

Linear Mode
Threaded Mode