Hot answers tagged

19

We can start by importing the file as an XMLObject: $url = "https://dl.dropboxusercontent.com/u/1012958/iTunes%20Library.xml"; $xml = Import[$url, {"XML", "XMLObject"}]; Short[$xml, 4] (* XMLObject[Document][ { XMLObject[Declaration][Version->1.0,Encoding->UTF-8] , XMLObject[Doctype][plist,Public->-//Apple Computer//DTD PLIST 1.0//EN,&...


15

I think Mathematica with its symbolic and pattern-matching capabilites is well suited to tackle parser and tree searching. It's better to check, though. Here are two quick proof-of-concept of the toy engine. One ad-hoc, the other using functional parsers. It' s readily apparent that every simple selector can be mapped to some form of XMLElement pattern: "...


9

I'm going to focus on the convenience part of the question. jsoupLink can work well for a lot of situations. Let's say, for example, that we want to parse RSS. RSS is an XML format, so we can do it like this: << jSoupLink` root = Import["http://packagedata.net/index.php/feed", "HTMLDOM"] items = root["Select", "item"]; What this shows is that using ...


8

This is the reply from Wolfram Research Technical Support [CASE:631799]: After consultation with a developer, there is nothing really that can be done with the Export function directly in terms of simplifying the group structure of SVG files. You may be able to develop some system by programmatically parsing the file down, but this would likely not ...


8

JLink` Here is a solution that uses JLink` to leverage Java's support of XML Schema: Needs["JLink`"] InstallJava[]; LoadJavaClass["javax.xml.validation.SchemaFactory", AllowShortContext -> False]; LoadJavaClass["javax.xml.XMLConstants", AllowShortContext -> False]; validateXml[xsd_String, xml_String] := JavaBlock @ Module[{factory, xsdSource, ...


7

If you are familiar with AppleScript, you could try an approach like this: (* from http://github.com/fmeinberg/AppleScript *) AppleScript["RunFile", file_] := Run["osascript " <> file] AppleScript["RunScript", script_] := Block[{file = ToFileName[$TemporaryDirectory, "script.txt"]}, Export[file, script, "String"]; AppleScript["RunFile", file]] ...


7

I can't try it out because your snippet lacks some brackets, but perhaps this will do the trick: Cases[snippet, XMLElement["designInfo", {"studyType" -> atrib_, ___}, ___] :> atrib, \[Infinity]]


6

I am using Mathematica 8.0.4 and have no URLFetch command, so I have used Wget to download this 5915505 bytes file: wget http://uscode.house.gov/popularnames/popularnames.htm After downloading I evaluated popNamesXML = Import["popularnames.htm", "XMLObject"] And got the XMLObject without any errors. Then I Imported this file as "Text" and tried to get ...


6

Here's an easy way: body //. XMLElement[_, _, t_] :> t // Flatten // StringJoin


6

It's not clear how XML can be converted to JSON in a general way, because how would one deal with attributes? You happen not to have attributes in your XML, but that's just a special case. Luckily, Mathematica is a great language to write ad hoc parsers in. convert[XMLObject["Document"][{}, content_, {}]] := convert[content] convert[XMLElement[tagName_, _, ...


6

You can capture all consecutive events like this: xmldata1 = Cases[xmldatatest, XMLElement[ "series", {}, {XMLElement[ "header", {}, {XMLElement["type", {}, {aa_}], XMLElement["moduleInstanceId", {}, {bb_}], XMLElement["locationId", {}, {cc_}], XMLElement["parameterId&...


5

I don't know what causes the issue (which looks like a bug to me), but I generally find the best way to deal with exerting control over Import not doing quit the right thing with HTML is to use the "XMLObject" element and use ordinary Mathematica functions on it, like so: In[1]:= ImportString@ ExportString[ ImportString["a<span>b<...


5

In this case what you want is simple/regular enough that you can define the XMLElement etc. as functions that return what you want inside a Block: Block[{XMLObject, XMLElement}, XMLObject["Document"] = #2 &; XMLElement["tekst", {"write" -> t_}, data_] := {t, Sequence @@ data}; XMLElement["lu", attr_, data_] := "id" /. attr; XMLElement[tag_, attr_,...


5

First, let me tell you that following your code my output is quite different. g=Graphics[{Disk[{-1,0},1/2],Rectangle[]}]; ExportString[g,"SVG"] Output <?xml version="1.0" encoding="UTF-8"?> <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="361pt" height="223pt" viewBox="0 0 361 223" version="1.1"> <g ...


5

If you look at the URL that you found, you'll notice a parameter pn. If you change this parameter you get another set of t-shirts. To get all t-shirts you need to download that URL for all allowed pn. Let's say pn stands for "page number". Now, how many pages are there? We have to remember that this is part of an API. Another programmer used the response of ...


5

To get the ball rolling, here's my incomplete parser. And here's a minimal markdown sample. This is what my markdown parser makes of it, used like so: CloudExport[ MarkdownToXML[ Import["https://raw.githubusercontent.com/b3m2a1/mathematica-BTools/\ master/Resources/Templates/PacletServer/content/pages/About.md", "Text" ], "...


4

This is one way. Using data=xmlInfo nodes=Cases[data,XMLElement[{_,"Object"},{"ObjectType"->"Target","Name"->target_,"Id"->id_},{x__}]:> {target,id,{x}} ,\[Infinity]]; getColors=Cases[#,XMLElement[{_,color_/;MemberQ[{"Cyan","Yellow","Black","Magenta"},color]},{},{value_}]-> {color,value},\[Infinity]]&; nodes[[All,3]]=getColors/@nodes[[All,...


4

This is not a complete answer but has too much for a comment. I don't have Numbers so do not know what is in the file you linked but the following information can be obtained: Import["PathTo/example.numbers", "ZIP"] (* {"QuickLook/Thumbnail.jpg", "buildVersionHistory.plist", "index.xml"} *) Now grab the XML: Import["PathTo/example.numbers", {"ZIP","...


4

This seems to be a normal way to extract the data:- xml = XMLObject["Document"][{XMLObject["Declaration"] ["Version" -> "1.0", "Encoding" -> "UTF-8", "Standalone" -> "yes"]}, XMLElement["file", {"name" -> "filename.xml"}, {XMLElement["tekst", {"write" -> "test1"}, {XMLElement["relation", {"type" -> "co-synonyms", "...


4

Had to add the "ReadDTD" -> False option to $xml = Import[ "/Volumes/WDC3TBRAID/Downloads/iTunes Music Library.xml", {"XML", "XMLObject"}, "ReadDTD" -> False] to get WReach's solution to work with Mathematica 11.0. Rest of his solution works fine after that.


4

This problem appears to be localized to HTMLFragment, possibly because HTML specifications before HTML5 did not use self-closing tags like XHTML and XML always have. Exporting the string as XML appears to solve the problem: ExportString[XMLElement["test", {"attr" -> "val"}, {}], "XML"] "<test attr='val' />"


4

I think I can understand this latter version of the question better. Unfortunately though I don't speak jquery, so I don't really understand your example, therefore I will leave the applications to you, and limit this answer to getting an XML form of a Mathematica expression. As it happens, achieving something along those lines is pretty simple, since ...


4

Try ExportString[ImportString["<mtext> </mtext>", {"MathML", "XMLElement"}, "NormalizeWhitespace" -> False], "XML"] From XML/tutorial/ImportingXML. I thought the default for "NormalizeWhitespace" was Automatic. However the documentation states that "True" is the default, where "all the whitespace inside an element is normalized (default)".


4

First of all, I think you can make your life a bit easier by using RawJSON as input format specification. It basically makes Mathematica use Associations where appropriate when importing JSON. url = "http://finance.services.appex.bing.com/Market.svc/ChartDataV5?symbols=126.1.BY.NYS&chartType=1y&isEOD=False&lang=en-US&isCS=true&isVol=true"...


4

To make this work, you need to specify the format. There is no "XMLObject" format. "XMLObject" is an import element for the "HTML" and "XML" (and perhaps some other) formats. Thus when you only specify "XMLObject", Mathematica is still trying to auto-detect the format. In this case, it will try to import the file as the Graph6 format, which won't work ...


4

Cases[xml,XMLElement["forename" | "surname", _, x_] :> x, \[Infinity]] (* {{"Adrian"}, {"Adgar"}, {"Babar"}, {"Barbados"}, {"Bode"}, \ {"Charly"}, {"Chimp"}} *) Edit Looks like we have to guess at what the desired output is so here is guess #2 with first names and surnames grouped. In the example provided first names always precede surnames in the ...


4

C.E.'s answer is probably the best, relying as it does on a well-constructed Java library with serious people behind it, but I thought it'd be nice to have a pure Mathematica solution as well, so I sunk some time into building an XMLGraph object which I put here. The basic idea is to store the two pieces of data I talked about before, a Graph that holds all ...


4

I don't know how extensible this is, but it works for your case, Cases[list, p[args:PatternSequence[___, a, _p, q, ___]] :> SequenceCases[ {args}, {a, p[x_, y_], q} :> {x, y} ], Infinity ] (* {{{1, 2}, {3, 4}, {5, 6}}} *)


4

I've not been involved with XML lately, but I used to (re)define XMLObject and XMLElement to process an XML structure. It's usually received lukewarm appreciation on this site, but it seems like the sort of expression-rewriting Mathematica was built for. And in this case, it's easy: json = Block[ { XMLObject = Function[{#2} &], , XMLElement = #1 ...


4

Expanding my comment into a concrete answer: Using GeneralUtilities`PrintDefinitions to look at the functions responsible for the "MathML" export1, it looks like the core function is System`Convert`MathMLDump`BoxesToSMML: (* remove limit on number of definitions *) DownValues@GeneralUtilities`PrintDefinitions = DownValues@GeneralUtilities`...


Only top voted, non community-wiki answers of a minimum length are eligible