Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I am trying to convert a bunch of text files into structured XML files. Using string expressions I have extracted data, so I have:

title={"https://www.ometz.ca/event/abiletes-dentrevue-1-678/?langID=1"};`
date={2011,7,14,13,22,22.`};`
content=": text/html; charset=UTF-8 Ometz A community of services for life Employment Job Seekers Job Listings Job Seeker Registration Career Counselling Employers Cocktails & Conversation Candidate Profiles Employers - Submit a Job Posting ProMontreal Entrepreneurs Fund ProMontreal Entrepreneurs Mentors Calendar You & Yours Community Assistance Counselling Services Administered Funds Orthodox Community We";`

So far I have done the following to generate XML objects, for title, date, and content in turn. It is a bit messy but seems to work.

a = ExportString[
  XMLElement[
   "title", {}, {StringReplace[
     ToString[title], {"{" -> "", "}" -> ""}]}], "XML"]

b = ExportString[
  XMLElement[
   "date", {}, {StringReplace[
     ToString[date], {"{" -> "", "}" -> ""}]}], "XML"]

c = ExportString[XMLElement["body", {}, {content}], "XML"];

The output is thus:

<title>https://www.ometz.ca/event/abiletes-dentrevue-1-678/?langID=1</title>
<date>2011, 7, 14, 13, 22, 22.</date>
<body>: text/html; charset=UTF-8 Ometz A community of services for life Employment Job
Seekers Job Listings Job Seeker Registration Career Counselling Employers Cocktails &amp;
Conversation Candidate Profiles Employers - Submit a Job Posting ProMontreal Entrepreneurs
Fund ProMontreal Entrepreneurs Mentors Calendar You &amp; Yours Community Assistance
Counselling Services Administered Funds Orthodox Community We</body>

So far so good, but I need to get it exported to an XML file, say text.xml.

For some reason I'm hitting a wall in how to get this working, despite reading docs & some supplementary material. How would you do this? (also, any advice on the above code is appreciated)

share|improve this question
1  
Have you looked at Export? –  rcollyer Aug 1 '13 at 14:36
    
@rcollyer I have. I can generate XML files, but my internal brackets get replaced so I get code like this: ` <String>&lt;title&gt;ometz.ca/event/abiletes-dentrevue-1-678/?langID=1&lt;/…;` –  programming_historian Aug 1 '13 at 14:39
    
A couple of things. You're missing a root element, and it is necessary to be valid xml. That aside, you can always pick the format, so you can export a string as xml via Export["file.xml", string, "Text"] which won't escape the brackets. However, once you have a root element, I suggest crafting an XMLObject and build your document inside it. Then, you can Export it with ease. Also, look at this tutorial. –  rcollyer Aug 1 '13 at 15:09
    
@rcollyer Thanks. I've been struggling with this documentation all morning. I'll stick with it and hopefully figure it out, following your instructions. It's appreciated. –  programming_historian Aug 1 '13 at 15:34

1 Answer 1

Thanks to rcollyer's comments and some of the documentation (which was confusing to a person new to this side of things, i.e. this), I figured this out. On the off chance that anybody stumbles along here and has the same issue, here is what worked for me.

Export["test.xml", 
 XMLObject["Document"][{}, 
  XMLElement[
   "website", {}, {XMLElement[
     "title", {}, {StringReplace[
       ToString[title], {"{" -> "", "}" -> ""}]}], 
    XMLElement[
     "date", {}, {StringReplace[
       ToString[date], {"{" -> "", "}" -> ""}]}], 
    XMLElement["content", {}, {content}]}], {}]]

I'll still leave this unanswered in case somebody has a better answer. :)

share|improve this answer
1  
If you have a lot of these (for me that is more than 2), I'd set it up as a function with title, date, and content as arguments. –  rcollyer Aug 1 '13 at 16:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.