XML file reading error - xml-serialization

help me please i'm having the following issue:
I'm trying to read a XML file that looks like this:
<service />
<parameters>
<parametro nombreParametro="payment" valorParametro="<?xml version="1.0" encoding="utf-16"?>" tipoParametro="string" />
</parameters>
The xml file is well formed, but as you can see, i have an < < and > > characters as attribute on some elements and the problem is that when i tried to read the file like this:
xmlDoc.LoadXml(stringWithXmlFileContent);
It gives me the following error:
Additional information: '<', hexadecimal value 0x3C, is not a valid character or attribute. Line XX, position XX.
What should i do to avoid this error, i don't want to make a Replace cause i'm building a generic method.
Thanks in advance.

I can't reproduce this, assuming that what you posted was only a portion of the XML file. (If it was the whole file, then the problem is it doesn't have a single root element, as Rubens said in his answer). Here's a short but complete program showing the same attribute value not having a problem:
using System;
using System.Xml;
public class Test
{
public static void Main(String[] args)
{
string xml = "<element attr=\"<?xml version="1.0""
+ " encoding="utf-16"?>\" />";
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
Console.WriteLine(doc.OuterXml);
}
}
Can you edit your question to include a similar program which does show the problem?

Seems your XML file isnt well-formed, as you must have a single root element.
Can you tell us how are your building this file?

Related

Saving to yml file using Spigot

I'm attempting to produce a Message.yml file using Spigot's YAMLConfiguration.
This is my code:
public static void create() {
if(messagesFile.exists()) return;
try {
messagesFile.createNewFile();
messages.options().copyDefaults(true);
messages.addDefault("MESSAGES.PREFIX", "&c[YourServer] ");
messages.addDefault("MESSAGES.DESIGN", "§8§l- ");
messages.addDefault("MESSAGES.NOPERMS", "§c§lDazu hast du keine Rechte!");
messages.addDefault("MESSAGES.ADDMAP.USAGE", "§c§lBitte nutze /addmap [mapname]!");
messages.save(messagesFile);
} catch(Exception e) {
e.printStackTrace();
}
}
However, the config.yml file I received after running it read as follows:
MESSAGES:
PREFIX: '&c[YourServer] '
DESIGN: "\xa78\xa7l- "
NOPERMS: "\xa7c\xa7lDazu hast du keine Rechte!"
ADDMAP:
USAGE: "\xa7c\xa7lBitte nutze /addmap [mapname]!"
Is there any way to fix it?
It thinks the text is a string and not a standalone character.
https://www.spigotmc.org/threads/special-characters-in-config.298138/
Yeah u use special caracter to save the color but it's a String. Don't put your color here just save the String. When you want to resend the text from the config just put for example.
player.sendMessage(ChatColor.RED + config.get("MESSAGES.PREFIX"));
this is just an example
Like #Minecraft said in his answer, the issue is that Java is recognizing the § as a part of your string and translating it to unicode.
What I would do is have your custom config file stored in your plugin resources directory with all the default values you want it to have already defined.
Then when you want to use the custom message, get it from the config file using getConfig()'s returned value's methods. Then, if you want to support color codes, you should use message = ChatColor.translateAlternateColorCodes('&', yourMessage); or something along those lines. Should be plenty to get you going.
Also, be sure and use a unified symbol for these color codes (default is &), but you can set it in the aforementioned method translateAlternativeColorCodes(). You seem to be using & or §, I would stick to &.
Sources:
https://www.spigotmc.org/wiki/config-files/#using-custom-configurations
https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/ChatColor.html#translateAlternateColorCodes(char,java.lang.String)

NSDocument XML read Issue

I am working on a NSDocument based Mac app. Which imports .xml file. It's working fine for some xml files but for few having issues.
Issue is read() is modifying the data when we import file, i need to keep the original data as it is.
what do i need to do to make sure i get original xml data in the read()?
I am using below function to read the file
override func read(from data: Data, ofType typeName: String) throws {
var error:NSError? = nil
var xmlDocument1:XMLDocument? = XMLDocument()
do{
xmlDocument1 = try XMLDocument(data: data, options: XMLNode.Options(rawValue: XMLNode.Options.RawValue(Int(XMLNode.Options.nodePreserveWhitespace.rawValue))))
}catch let err as NSError{
error = err
}
if error != nil {
throw error!
}
}
and i parse xmlDocument1 to read and get all the xml information.
Issue: Doing this way swift is modifying the document, as mentioned below.
Example 1:
Original:
<iws:attr-option name="1 - Poor" />
<iws:attr-option name="2 - Needs Improvement" />
Data getting from Read(), notice the closing tags added automatically
<iws:attr-option name="1 - Poor"></iws:attr-option>
<iws:attr-option name="2 - Needs Improvement"></iws:attr-option>
Example 2:
Original:
<source>
<ph id="12" x="</span>">{12}</ph>
</source>
Data getting from Read(), notice the ">" symbol is replaced with "& gt;"
<source>
<ph id="12" x="</span>">{12}</ph>
</source>
Example 3:
I am not able to paste the code here as the special character is not even displaying here, so adding image.
left is the original and right side one is what i am getting in read(), special character is missing.
Code Sameple : (I am not sure if we can post code directly here)
https://drive.google.com/drive/folders/1WWGE7fFJPKvs5KU5f_PlwWtoqCVxTcS0?usp=sharing
Above drive we have sample xml file and code.
"DevelopingADocumentBasedApp" is the code, just open the "DocumentBasedApp.xcodeproj", run it.
3 .Once it runs, click on Menu->File->Open and open the provided xml file.
In content.swift, Keep a break point at "print(xmlDocument!)"
Here we can see the document is modified by NSDocument, and it is different from the original
Edit:
#matt Thank you for making me understand real problem, Initially i thought that i have issue with NSDocument's read(). But issues is XMLDocument() not returning exact data. I need to find a solution for that.
Reading is not changing your document.
You make an xml document, with XMLDocument(data:...). You are asking for a new valid XML document based on your original, and that is exactly what you get. The resulting structure is not a big string, like your original data; it is an elaborate node tree reflecting the structure of your XML. That node tree is identical to the structure described by your original. That fact does not affect in any way your ability to parse the document; indeed, it is why you are able to parse the document. If you think it does cause an inability to parse the document, your parsing code is wrong (but you didn't show that, so no more can be said).
Also note that your evidence for what is "in" the XML document is indirect; the XML document is a node tree, but the strings you display are the output of a secondary rendering into a string. That rendering representation is arbitrary and malleable; it obeys its own rules of formatting. (And again, you didn't show anything about how you obtain that rendering. Perhaps we are talking about your print statement?)
The point is, you seem to have to some sort of expectation about how passing into an XMLDocument and then back out of it will "round trip" your original string in such a way that the output looks just like the original. That expectation is incorrect. That's not what XMLDocument does.
And merely reading the original data into an XMLDocument did not change the data, I can promise you that.
So don't worry, be happy; as far as the validity of your XML is concerned, everything is fine, and the data you started with has not been altered in any way.
Here's a demonstration:
let xmlstring = """
<testing>
<fun whatever="thingy" />
</testing>
"""
print(xmlstring)
let xmldata = xmlstring.data(using: .utf8)!
let xml = try? XMLDocument(data: xmldata, options: [])
print("=======")
print(xml!)
The output is:
<testing>
<fun whatever="thingy" />
</testing>
=======
<?xml version="1.0"?><testing><fun whatever="thingy"></fun></testing>
As you can see, the output from the print is not the same as the input string. But it is a valid XML representation of the original string, and that's all that matters. And the original xmlstring and xmldata that I started with are, I assure you, completely untouched.

apigee overwriting the target.url with a custom variable

I have been trying to overwrite the target.url with a variable using the Assign Message Policy. Per other solutions, I have put this in the "Target EndPoint" Section. The issue is, unless I hard-code the root section of the URL, the substitution fails. I have tried all the commented VALUE stmts below with and then started adding the "REF"stmts to attempt to solve the issue - to no avail. You can see I have tried cutting the target into various snippets using Extract policies, but cannot get a solution that works.
Thanks for help.
For the purposes of the code snippet below
entireURL = "http://my.root.url/thestuff/morestuff"
AppServerURL = "my.root.url/thestuff/morestuff"
AppServerRoot = "my.root.url"
AppServerSfx = "thestuff/morestuff"
codee from Assign Message Policy
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage async="false" continueOnError="false" enabled="true" name="Post-to-named-serverL">
<DisplayName>Post to named server</DisplayName>
<FaultRules/>
<Properties/>
<AssignVariable>
<Name>target.url</Name>
<Value>http://${AppServerRoot}/{AppServerSfx}</Value>
<Ref/>
<!--
<Value>http://my.root.url/{AppServerSfx}</Value> works but I need the root changed
<Value>http://{AppServerRoot}/{AppServerSfx}</Value>
<Value>http://${AppServerRoot}/{AppServerSfx}</Value>
<Value>http://{AppServerURL}</Value>
<Value>http://${AppServerURL}</Value>
<Value>entireURL</Value>
<Value>{entireURL}</Value> -- this was my first try
<Value>${entireURL}</Value>
<Ref>entireURL</Ref>
<Ref>{entireURL}</Ref>
<Ref>${entireURL}</Ref>
-->
</AssignVariable>
<IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables>
<AssignTo createNew="false" transport="http" type="request"/>
</AssignMessage>
You are correctly putting the target.url manipulation in the Target Request flow.
Using AssignMessage/AssignVariable can be limiting. The Value element doesn't allow you to do any variable substitutions.
The following worked for me:
<Ref>entireURL</Ref>
Ref also doesn't allow variable substitutions -- it just takes the name of the variable. Since you have to build the value of that variable ahead of time, using the Ref example above doesn't buy you much.
I usually accomplish target URL rewriting using a JavaScript callout with code similar to the following:
var appServerRoot = context.getVariable("AppServerRoot");
var appServerSfx = context.getVariable("AppServerSfx");
context.setVariable("target.url", "http://" + appServerRoot + "/" + appServerSuffix);

What does the message "Invalid byte 2 of a 3-byte UTF-8 sequence" mean?

I changed a file in Orbeon Forms, and the next time I load the page, I get an error message saying Invalid byte 2 of a 3-byte UTF-8 sequence. How can I solve this problem?
This happens when Orbeon Forms reads an XML file and expects it to use the UTF-8 encoding, but somehow the file isn't properly encoded in UTF-8. To solve this, make sure that:
You have an XML declaration at the beginning of the file saying the file is in UTF-8:
<?xml version="1.0" encoding="UTF-8" ?>
Your editor is XML-aware, so it can parse the XML declaration and consequently use the UTF-8 encoding. If your editor isn't XML aware, and you don't want to use another editor, look for an option or preference allowing you to specify that the editor must use UTF-8.
A three byte UTF-8 sequence looks like:
1110xxxx 10xxxxxx 10xxxxxx
Your error message may mean that the first byte of the three is incorrectly flagging the start of a three byte sequence or else that the second byte is malformed.
As #avernet says, you need to make sure that all elements in your system are producing and expecting UTF-8.
When you start your program, use the following Java command line argument:
-Dfile.encoding=UTF-8
For example,
java -Dfile.encoding=UTF-8 -jar foo.jar
I got the same problem in Eclipse, I just tried by changing the file type.
Right click on file -> Resource -> Text file encoding (UTF-8)
This solution worked for me.
Thanks.
I am using Eclipse and I also had to change the Text file encoding in:
->Windows->Preferences->Workspace
Then it worked fine.
Thanks
You might need to configure your Tomcat with the following parameter:
-Dfile.encoding=UTF-8
Had same problem.
Problem > I'm getting X509 certificate values (multiple encoding source) to generate a PDF report.
The PDF is generated throught a webservice that waits for an UTF-8 xml request and I've to reencode the values before marshalling.
Solution >
http://fabioangelini.wordpress.com/2011/08/04/converting-java-string-fromto-utf-8/
Using this class:
public class StringHelper {
// convert from UTF-8 -> internal Java String format
public static String convertFromUTF8(String s) {
String out = null;
try {
out = new String(s.getBytes("ISO-8859-1"), "UTF-8");
} catch (java.io.UnsupportedEncodingException e) {
return null;
}
return out;
}
// convert from internal Java String format -> UTF-8
public static String convertToUTF8(String s) {
String out = null;
try {
out = new String(s.getBytes("UTF-8"), "ISO-8859-1");
} catch (java.io.UnsupportedEncodingException e) {
return null;
}
return out;
}
}
Usage:
//getSummaryAttMap() returns a HashMap
String value = (String) getSummaryAttMap().get(key);
if(value != null)
value = StringHelper.convertToUTF8(value);
else
value = "";
I'll provide a special coding answer. When you check the xml file and there's nothing wrong, and you're using Java and running Tomcat Server. Your source code may neglect specify the encoding yourself, and thus JVM uses default encoding when read in xml contents as string or something else that repesents string, which in turn refer to Tomcat's default encoding. If encoding of xml and Tomcat are inconsistent, it might also report same error message.
The switching of the encoding for the input might help:
XMLEventReader eventReader =
inputFactory.createXMLEventReader(in,
"utf-8"
//"windows-1251"
);

Building DOM with xerces and Java - how to prevent escaping of ampersand

I am using xerces in Java to build a DOM. For one of the fields that becomes a text node in the DOM, the data is being delivered from a source that has already turned any non ASCII and/or XML special characters into their entity names or numbers, e.g. "Banana®"
I know the design of the system is wrong in terms the data source shouldn't be doing this but that is out of my control, but what I am wondering is if there is a way to somehow prevent this from being escaped and turned into "Banana&#174;" without decoding first? (I know it will implicitly convert any chars it needs to so I could enter the raw char after decoding).
Example code:
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document dom = db.newDocument();
Element root = dom.createElement("Companies");
dom.appendChild(root);
Element company = dom.createElement("Company");
Text t = dom.createTextNode("Banana®");
company.appendChild(t);
root.appendChild(company);
DOMImplementationRegistry dir = DOMImplementationRegistry.newInstance();
DOMImplementationLS impl =
(DOMImplementationLS)dir.getDOMImplementation("LS");
LSSerializer writer = impl.createLSSerializer();
LSOutput output = impl.createLSOutput();
output.setByteStream(System.out);
writer.write(dom, output);
Example Output:
<?xml version="1.0" encoding="UTF-8"?>
<Companies><Company>Banana&#174;</Company></Companies>
If you could somehow declare it in a CDATA section, it should be passed through as is.