-
Notifications
You must be signed in to change notification settings - Fork 182
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
json2xml sanitizes even when sanitize is set to false. #26
Comments
Yes, I have a similar issue, if not exactly same. I think the An exampleThe original xml: <title>Support and resistance &</title> If you use {
type: 'text',
text: 'Support and resistance &'
} And then, the generated xml from this JS object will be: <title>Support and resistance &amp;</title> So, basically, I think there needs to be an option to tell the library treating anything inside 'text' as it is, never escape or parse anything inside. |
I agree to both comments. |
I have published v1.4.0 to fix this issue. I use the answers in this StackOverflow question as the basis for the fix. Previously, when converting from
But since this approach causes issues as you demonstrated, now I am adopting the recommendation in this answer.
But note that I am not escaping The new implementation can cause a slight breaking change:
|
@adamgcraig, Regarding
This library assumes xml input and json input are well-formed documents without issues. |
@nashwaan Thanks. I will test the new version with my use case when I have time. |
I already tested it, right now the version 1.4.1 works great for my case ; P |
Seems this issue is resolved, I will close it. |
Sorry to open a closed can of worms, but I'm finding an issue that the '<' '>' characters, when they are functioning as a string in an attribute aren't being converted back to < and > when going to xml from json. Any ideas? Thanks |
@austin-laney , can you provide a simple and complete example, please? You might also want to check Custom Processing Callbacks as a quick solution to handle this issue. Look for But if you can provide a clear example, I might consider changing the core behavior of handling special characters in attribute values. |
@nashwaan, Thank you for the swift reply. <parser start="^\s*?<name>regex</name>$"> The start attribute denotes where a parser will begin extracting data and it's value is a regex. When I analyze how it's performing whilst debugging, here is the value when broken at a point to see the xml-js value: It does parse the tags into their respective tokens but on the conversion to js->xml it does not change it back. Any help is appreciated! Also, if I'm not mistaken the attributeValueFn would be supplied in the options correct? I tried that and it doesn't seem to be available. |
This library uses sax for parsing xml. It always converts predefined XML entities in attributes (like But, to convert json back to xml, you can use something like this: var convert = require('xml-js');
var xml = '<parser start="^\\s*?<name>regex</name>$"/>';
var js = convert.xml2js(xml, {compact: true});
var xml_ = convert.js2xml(js, {compact: true, attributeValueFn: function(value) {
return value.replace(/</g, '<').replace(/>/g, '>');
}});
console.log('xml -> js -> xml was ' + (xml === xml_? 'ok' : 'not ok')); |
Does jsontoxml support the attributeValFn? It does not recognize that when I attempt to build. |
It should be |
@nashwaan I have a use case that is very related to this issue -- I hope this is a good place to ask the question. I am using js2xml, and trying to encode the & within xml attribute values. However, since the " character is encoded by default, and encoded before the attributeValueFn is executed, replacing & causes " to be converted to &quot; . Is it possible to execute the attributeValueFn before converting " to " ? Alternatively, can an option be re-added to js2xml to convert the 5 special characters within attribute values? My current workaround is to use this attributeValueFn:
Since xml2js already converts the escape sequences back to special characters, I feel this should be a suitable use-case to close the loop. Please let me know if any more detail would help! Thanks |
@nashwaan , as @bidiu mention, so will you add I'm generating some kind of view file written in XML: <view wx:if="{{length > 5}}"> </view> The greater than operator |
I am still running into problems because of this |
@nashwaan I am using xml-js library aggressively. Due to this issue, I spent time more time on r&d. Can you please resolve this issue ASAP? |
I face a similar issue, where Input <Title>>400</Title> Output <Title>>400</Title> |
noticed this issue loading Adobe Illustrator SVGs.
Error returned
|
Also, xml2json crashes if it encounters an unescaped <, >, or & in the XML, but I am not sure whether you would consider that a bug.
Input:
Code:
Output:
XML:
compact JSON:
back-converted XML from compact:
matches original:
false
verbose JSON:
back-converted XML from verbose:
matches original:
false
Saved backfromcompact.xml.
Saved backfromverbose.xml.
The text was updated successfully, but these errors were encountered: