I have a jsp page of elements mapped with XML mapping. One of the textarea field is parsing the elements if you enter > or < special characters to > and < and &. This is giving problem while parsing XML elements.
suppose IF i have an textarea field name like <Explain> . Then i have entered the value as < as given here: <Explain> < </Explain>. Here the field is converting to &/Explain> How can i avoid this problem.
I have a code in my java file like this.
if(elementStr.indexOf("&") != -1)
elementStr = elementStr.replaceAll("&[^a][^m][^p][^;]", "&");
How can i avoid parsing the special characters to symbols.
You can simple use this:
${fn:escapeXml(elementStr)};
where is your String
Related
I am using jdom 2.0.6 version and received this IllegalDataException:
Error in setText for tokenization:
it fails on calling the setText() method.
Element text = new Element("Text");
text.setText(doc.getText());
It seems some characters in 'text' it doesn't accept. For two examples:
Originally Posted by Yvette H( 45) Odd socks, yes, no undies yes, no coat yes, no shoes odd. 🏻
ParryOtter said: Posted
Should I specify encoding somewhere or for some other reasons?
In fact you just have to escape your text which contains illegal characters with CDATA :
Element text = new Element("Text");
text.setContent(new CDATA(doc.getText()));
The reverse operation (reading text escaped with CDATA is transparent in JDOM2, you won't have to escape it back).
For my tests I added an illegal character at the end of my text by creating one from hex value 0x2 like that :
String text = doc.getText();
int hex = 0x2;
text += (char) hex;
I have several files containing the following XML element:
<table cellpadding="0" cellspacing="0" border="0"style="width:100%">
The part that says border="0"style=" needs a space between the 0 value and style attribute.
Unfortunately there are too many files with this issue to make manually going and inserting the space a viable option.
I can edit attributes and I can edit values by creating an Xpath that gets the table as a NodeList, creates a node and gets the attributes.. but how would I add a space between the attribute and the value??
We could always just String.split("\""); aka split on the commas.
Here, try this:
/** In reality, you would probably read file to string?
* or read line by line? either way is an easy fix!
*/
String input = ("<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\"style=\"width:100%\">");
String xmlTag = StringUtils.substringBetween(input, "<", ">");
Starting with index number, array after split contains as follows:
XML Tag Name
ODD INDICES ~ 1, 3, 5, and so on, contain: attribute name.
EVEN INDICES ~ 2, 4, 6, and so on, contain: attribute value.
int arrSize = xmlCharValPairs.length()
String[] xmlCharValPairs = xmlTag.split("\"");
StringBuilder sb = new StringBuilder(arrSize);
sb.append("<" + xmlCharValPairs[0] + " ");
for (int i = 1; i < arrSize-1; i++) {
if (i%2 == 0)
sb.append("\"" + xmlCharValPairs[i].trim() + "\" ");
else
sb.append(xmlCharValPairs[i]);
}
String returnXMLFormat = sb.toString();
This will leave you with an XML String in your requested format :)
If it's consistent length then all you need to write is a simple string parser that would add extra "" at X position.
If it's not the same everything I think I would try to check if char is " then a char -1 from it and then check if it's =" or (some letter)" for example a".
width="100" vs width="100" anotherparam="...
This could tell you if it's begining or end of param. If it's the ending then simply add a space char after it.
Obiously you could then check if it's "(someletter) or "(space) to know if there is space char after your apostrophe.
width="100" param2="..." vs width="100"param2=""
If you have lets say 200 files to edit you could use something similar to this:
File folder = new File("your/path");
File[] listOfFiles = folder.listFiles();
Then simply open files in a loop, edit them and save them to new files with their orginal names or just overwrite current files. It's up to you.
Your file isn't well-formed XML so you will need a tool that can handle files that aren't well-formed XML. That rules anything in the XSLT/XQuery/XPath family.
You can probably fix nearly all occurrences of the problem, with low risk of adverse side effects, by using a regular expression that inserts a space after any occurrence of " that isn't immediately preceded by =. (This will add some unnecessary spaces, but the XML parser will ignore them.)
I got a special character from ASCII value and created a presentation by inputting that character using docx4j library. If I want to print "£" mark it print with "£". Is there a special way to input special characters to the PPT.
I used following code.
String iChar = new Character((char)163).toString();
t.setTextContent(iChar);
Please unzip your pptx, and have a look at the content of the slide. It should contain something like:
<a:t>£</a:t>
You can create a p containing that with:
// Create object for p
CTTextParagraph textparagraph = dmlObjectFactory.createCTTextParagraph();
textbody.getP().add( textparagraph);
// Create object for r
CTRegularTextRun regulartextrun = dmlObjectFactory.createCTRegularTextRun();
textparagraph.getEGTextRun().add( regulartextrun);
regulartextrun.setT( "£");
or by unmarshalling a string. In either case, you can just provide the £ char directly.
You can generate suitable code using the docx4j webapp at http://webapp.docx4java.org/
I am converting a JSON Array to a XML string:
JSONArray json = new JSONArray(response);
xml = XML.toString(json);
and unfortunately the result contains nodes like
<24x24>blah</24x24>
Afterwards I want to create a 'real' XML Node with SAXBuilder which produces following error:
The content beginning "<2" is not legal markup. Perhaps the "2" ( ) character should be a letter.
Does anybody know how to remove this illegal markup from the XML String?
Maybe a regex which replaces <24x24>blah</24x24> with <t24x24>blah</t24x24>?
Thank you!
You can try using String.replaceAll() method with regex. Live demo
System.out.println("<24x24>blah</24x24>".replaceAll("(<\\/?)(?=\\d)", "$1t"));
output:
<t24x24>blah</t24x24>
I have some xml that looks like this:
<xml><name>oscar</name><race>puppet</race><class>grouch</class></xml>
The tags change and are variable, so there won't always be a 'name' tag.
I've tried 3 or 4 parses and they all seem to choke on it. Any hints?
Just because it doesn't have a defined schema, doesn't mean it isn't "valid" XML - your sample XML is "well formed".
The dom4j library will do it for you. Once parsed (your XML will parse OK) you can iterate through child elements, no matter what their tag name, and work with your data.
Here's an example of how to use it:
import org.dom4j.*;
String text = "<xml><name>oscar</name><race>puppet</race><class>grouch</class></xml>";
Document document = DocumentHelper.parseText(text);
Element root = document.getRootElement();
for ( Iterator i = root.elementIterator(); i.hasNext(); ) {
Element element = (Element) i.next();
String tagName = element.getQName();
String contents = element.getText();
// do something
}
This is valid xml; try adding an XML Schema that allows for optional elements. If you can write an xml schema, you can use JAXB to parse it. XML allows for having optional elements; it isn't too "strict" about it.
Your XML sample is well-formed XML, and if anything "chokes" on it then it would be useful for us to know exactly what the symptoms of the "choking" are.