I have an XML file with music albums that looks like this:
I want to extract the elements from the XML using Qt C++. Now I know how to extract the Author, or the Genre for example, which would be to get a QDomNodeList of a certain tag, then a QDomNode from that list at a certain index, and then converting that node to an element, and reading its attributes.
What I don't know is how do I extract the Songs? I have tried getting a QDomNodeList with elementsByTagName("Songs") from the root element, which gives me a node list of all the <Songs> in the XML, but then I can't again get a QDomNodeList of the <Song> tag, because it throws a conversion error.
Related
I'm having some trouble extracting info from a Python object. Basically, using notation like this works to get down to values within a dict I am working with:
clean_content['Al38zGKg6YC4']['image']
I was expecting to see another nested dict which contained the key/value that I wanted to extract. However, what's there is a list that looks like a dict[1]
I'm looking to extract the 'permalink' field from this list, and then tie it back to the page ID in the original dict. Any suggestions?
I came to a working solution as follows: converting the original dictionary into a pandas dataframe, I extracted the column containing the lists of image data. Each of these lists contained URLs for multiple images, so I extracted the one required using dicts:
image_df_temp = {}
image_df_url = {}
for i in range(len(image_df_base.index)):
image_df_temp[i] = pd.DataFrame(image_df_base.image[i])
image_df_url[i] = image_df_temp[i].iloc[0]
Finally, I edited the dicts missing the required metadata using .get:
for i in range(len(image_df_base.index)):
image_df_url[i] = image_df_url[i].get('permalink', "No image available")
What I have:
A CSV which I populated with rows of text, which are one word per cell.
Micro level: Attempting:
I am trying to create a dictionary where each row is a Key and each Key is assigned an empty list as a variable (see below).
I can do this one row at a time by converting the list to a tuple -->
creating an empty list -->
Adding the tuple to my dictionary as a key and assigning the empty list as the variable
However, I would like to do this in an automatic fashion as doing this individually is tedious.
Macro level: Attempting:
I want to assign a list of keywords (tags) to each row in my CSV to call upon the text later based on their tags.
My question:
Is there a way to do this the way I am describing?
Am I going about it wrong and should be doing this a different way?
*edit: I am thinking that if I flip this I could solve my overall issue.
For example make x amount of tags as key values for my tag dictionary and make a one time run through to assign each key with a empty dictionary value. Then populate the dictionaries with the text from my CSV.
This would not remove the one by one method; however, would reduce the amount of times I would need to enter Key/Value pairs as I am more likely to have more text than tags.
see code below
!#Python3
import csv
import os
import string
#open CSV and assign var to the list content
outputFile = open("output.csv", encoding="utf-8")
outputReader = csv.reader(outputFile)
data = list(outputReader)
#Get rid of empty cells
for list in data:
for object in list:
while "" in list:
list.remove("")
#open a dictionary
tags = {}
#Turn first row of CSV into a tuple
article1 = tuple(data[1])
#generate empty list
article1_tags = []
#Assign empty list as a variable to the article1 Key and put in tags dictionary
While True:
if article1 in tags :
break
else:
tags[article1] = article1_tags
Now that I have bit more of idea of what you are trying to acheive I would suggest using a list of dictionary's. Each dictionary containing the data about each article (or row from your csv file). The key here is that a csv file is still a plain text file, there is nothing special about csv. In fact I would avoid using excel altogether and edit using a text editor.
I would start by opening the file and reading each row (line) from the file into a key/value pair of a dictionary.
The cool thing about python 3 is that you do that very easily without extra modules.
csvfile = open('output.csv', encoding='utf-8')
articlelist = []
for line in csvfile:
articlelist.append(dict(textkey=line,tagskey=[]))
Using the iterator 'line' in this context with a text file stream object will automatically go row by row and take all the text of that line as a single string. So line is a string object here.
Once you have list of dictionary's like this you can simply iterate through the articlelist printing out or adding tags or doing whatever you wish even adding more key/value pairs to each dictionary. Doing it in this way means that not all the dictionary's need to follow the same format (although thats desirable).
I added the tagskey key and the value is an empty list which you can add to later.
Do not use infinite while loops or while loops at all to go through lists etc. Always use the
for iterator in theList:
method.
I would also look into using the JSON format for your little exercise here. I think it will lend itself much nicer to what you are trying to acheive. And with Python JSON is very easily read and then output again all using plain text. You could then output to a JSON text file manually edit it and then python read it again and process it.
I hope this helps.
I need to present a content editor interface that allows editors to select specific pages in order to generate a link list for website visitors.
It seems treelist/treelistEX is providing the expected interface and I have combined that with a source path to lock the editors to a start destination rather than the entire sitecore tree. Opted for treelist EX as this appears to be the most efficient way as it doesnt render the tree in full each time unless its called upon.
In terms of the output however I'm getting a pipe separated list of GUIDs- is this something I need to iterate through manually using linkmanager or some such to obtain the items title and its sitecore link? Or is there an existing process that manages such a multi-list and breaks it up into its components.
If anyone can provide an example of that code and how to draw out the title and URL that would be great.
Thanks
There is no built-in solution for getting title and url from the items selected in Treelist.
You can treat your Treelist field as a Multilist field (they both store just list of pipe separated IDs in the background) and use GetItems() method:
Sitecore.Data.Fields.MultilistField treelistField = Sitecore.Context.Item.Fields["myTreelistFieldName"];
Item [] selectedItems = treelistField.GetItems();
foreach (Item item in selectedItems)
{
string itemName = item.Name;
string displayName = item.DisplayName; // fallback to Name if not set
string title = item["Title"]; // assuming there is a field called Title
string url = LinkManager.GetItemUrl(item);
}
I'm using XSL in Umbraco to try and get a list of items associated with the current node. This list of items is built from the "Embedded Content" plugin content type. I've got this far:
<xsl:key name="key-name" match="listOfItems/data/item" use="itemKeyName" />
But this selects the listOfItems from all Umbraco nodes. Basically, I want the key to match $currentPage/listOfItems/data/item but as I can't use variables in the xsl:key, how can I achieve this?
There are several XSLT examples for Umbraco tree traversal here:
http://our.umbraco.org/wiki/reference/xslt/xpath-axes-and-their-shortcuts
I would like to be able to store reusable html-formatted text in Sitecore and reference in codebehind for inclusion in a custom user control. What is the best practice for doing this? If a user selects option A, for example, I would reference standard text A in my control. Any examples for how to accomplish this are appreciated. Thanks.
You have a couple options:
Store the text in the Standard Values of the same template that defines your option list. That makes it available on the same item, but standard for all items. Use security to lock down the field if you are worried about it being edited. This could also be accomplished with the new "cloning" feature in 6.4, I believe.
Create a structure outside of your Home element for storing this data. Based on the option selected, find an item in your content tree which corresponds to the selected item, and read the text off of it. You would need to find this item either relative to /sitecore/Content, or relative to your website root if multi-site support is a requirement.
No.2 in pseudo-code:
//get the item where we have the text values
Item textBase = Sitecore.Context.Database.SelectSingleItem(textBasePath);
//find the child w/ the same name as the selected option
Item textItem = textBase.Axes.GetChild(selectedOptionValue);
string value = textItem["text"];
I think I would do something like techphoria414's 2. option:
ie you have your normal "page" templates as per usual, but then you have some fields (multilist, treelist fields), where you put the source pointing to your other items contain the different texts.
then you basically just have to get the items from the current item (with some very quick'n'dirty code/pseudocode):
var CurrentItem = Sitecore.Context.Item;
Sitecore.Data.Fields.MultilistField mlf1 = CurrentItem.Fields["myExternalTexts"];
if(mlf1 != null)
{
foreach (Item itm in mlf1.GetItems())
{
lit += Sitecore.Web.UI.WebControls.FieldRenderer.Render(itm, "richtext");
}
}
You ofc shouldn't just add them to a literal and you should you Sitecore built in Field renderes if using Sitecore 6 or above and it's a Rich text field.
I hope that helps.