How to parse Nokogiri/libXML XML errors to human-friendly errors? - nokogiri

We are using Nokogiri to validate XML files using a XSD. The problem is that the error messages that Nokogiri generates are not very friendly and very hard to translate:
"Element '{http://www.portalfiscal.inf.br/nfe}infNFe': The attribute 'Id' is required but missing."
Does anyone know of a parser or any other way to capture the info needed from the error to generate a more human friendly error?
Until then, we will be doing a custom parser for them... ouch!

I created a gem for this that is now open source: https://rubygems.org/gems/xml_errors_parser
It seems to work pretty well so far, but number of errors parsed is very few for now. It is however very easy to add new errors, so we will be adding them as needed.
Code reviews and pull requests are always great :)

Related

JSON LD recognized by Google, but not Facebook pixel ( Ruby On Rails)

I have implemented a Json-ld dynamic creation process to boost my SEO. The JSON is created through the use of Jbuilder ( code is in a partial), rendered in a script tag with a type of "application/ld+json". All of it is wrapped up in a content_for, so that I can reuse the logic.
Once it has been implemented, I started to get this error in my console: "[Facebook Pixel] - Unable to parse JSON-LD tag. Malformed JSON found: ' "
I tested my Json-LD on the google structured data tool and everything came back ok.
I've added an hand written JSON-LD in my script tag, instead of my aforementioned logic,
everything looked ok. No error was displayed in the console, and Chrome Facebook Pixel
Helper was able to find my JSON-LD.
Bottom line, it appears that using my dynamic logic with the partials create a random " ' ", which makes no sense for me.
Any of you ever had the same issue, or something similar ?
May be templating engine is messing you up. You might consider using the json-ld gem to validate the output as part of continuous integration (you can also semantically validate the content using other gems).
I’ve had success using JSON-LD in Haml, but I just use to_json from a Hash hierarchy which has always worked well for me.

Grails - Read, customize and attach a word document

I need to do the following (with Grails):
Read a word template
Add user details (e.g. name, last name) to the document
Send the customized word by mail
I am thinking in Apache POI to read .doc, then in the document I will have keywords e.g."°!"#$" and make the customization.
Then I will create the document, send it (with Grails plugin) and erase the document from the server
Is this a good approach or I am forgetting something?
Thank you very much
EDIT:
DOCx4j: My docx operations are simple, so I can go with the "approach 1" in the variable replace,
but for some reason the next sample is not working for me:
https://github.com/plutext/docx4j/blob/master/src/samples/docx4j/org/docx4j/samples/VariableReplace.java
I'm getting a:
| Error 2014-03-27 09:10:23,640 [http-bio-9000-exec-5] ERROR errors.GrailsExceptionResolver - SAXParseException occurred when processing request: [POST] /Estrategia/pasantiaCorta/generarDocumento
Content is not allowed in prolog.. Stacktrace follows:
Message: Content is not allowed in prolog.
In the line:
// Approach 1 (from 3.0.0; faster if you haven't yet caused unmarshalling to occur):
documentPart.variableReplace(mappings);
Sure, you can use POI or docx4j. For either, you'd be much better off using docx rather than the legacy binary doc format.
For docx4j (my project), see creating-word-docx-documents-dynamically-from-grails
At your step 2 (add user details), there are a variety of approaches you can use.

NSXMLParserErrorDomain error 64

I have to call xml parsing I am receiving this error "NSXMLParserErrorDomain error 64", if I will parse dynamically I will get this error. statically parsing on that same xml I get my attribute values.
If i will try another one server different request,url and response it will working properly the xml parsing and i am getting attribute values.
Please check this link==>http://brandontreb.com/wordpress-for-iphoneipad-nsxmlparsererrordomain-error-64-resolved
*After scouring the internet, I found that this could be the result of a few issues.
1) Special characters in a post body that are not supported by NSXMLParser
2) Special characters in a comment
3) Invalid post or comment RSS
4) An error in a theme/plugin file
For me, this turned out to be an issue with the comments RSS feed. I loaded it up in the browser and long behold, even the browser threw an error. But what could be causing this? Turns out, I had left a space in a plugin that I created. This caused a space to be output at the beginning of the comments XML, causing it to error. Notice the space between ?> and
After removing the space from this plugin, I loaded up WordPress for iPhone and it added my blog without a problem.
So, the take away from this is don’t output spaces when you create a plugin.*
Hope this helps you...:-)
In my case, it was only the blank line at the beginning of the file that was causing the issue. Error code 64 is for misplaced XML declaration as you can see in the documentation:
NSXMLParserMisplacedXMLDeclarationError = 64,
Misplaced XML declaration.

How to strip the CSS code in Rails

I have the rails application which accepts the XML output from another application. For some condition the XML tage content come up with CSS code
For example :
<\/sample/> .headermenu{float:left;no-repeat right;font-size:0.75em; padding-bottom:3px}, #div{float:left} This is the test value from another site <\/sample/>
In my ruby application i have parse the XML content and display the content.
It start displaying CSS content like the above. I want to display strip the CSS code if exist in the content.
Is their any way . we can do this please help...
raw method might help you.It outputs data without escaping a string. Check here http://apidock.com/rails/ActionView/Helpers/RawOutputHelper/raw for more details.
I dont know if this is what you are looking for but you can try css parser by the way whenever you need a rails or ruby gem just search for it at rubygems

Get Source of XmlSerializer FormatException?

i'm deserializing some XML Data i get from a WebService using the XmlSerialzier. This worked quite good until now, but since some days i get an FormatException while the Serializer parses the XML.
Is there any Chance to get the Value/Element/Tagname the Serialzer tried to parse when the Exeption was thrown? I haven't found anything in the Exception Details.
Thanks and Greetings
Kornelis
If the format exception doesn't give you enough information to solve the problem, you can go one step further and debug the specific code that produces the problem. The XmlSerializer works by generating a assembly at runtime which is responsible for serializing/deserializing the type you want. This generated code can be debugged in the following way (check link).

Resources