1
$\begingroup$

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)

$\endgroup$
4
  • 1
    $\begingroup$ Have you looked at Export? $\endgroup$ Commented Aug 1, 2013 at 14:36
  • $\begingroup$ @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;/…>` $\endgroup$ Commented Aug 1, 2013 at 14:39
  • $\begingroup$ 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. $\endgroup$ Commented Aug 1, 2013 at 15:09
  • $\begingroup$ @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. $\endgroup$ Commented Aug 1, 2013 at 15:34

1 Answer 1

2
$\begingroup$

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. :)

$\endgroup$
1
  • 1
    $\begingroup$ 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. $\endgroup$ Commented Aug 1, 2013 at 16:43

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.