本文整理了Java中org.json.XML
类的一些代码示例,展示了XML
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。XML
类的具体详情如下:
包路径:org.json.XML
类名称:XML
[英]This provides static methods to convert an XML text into a JSONObject, and to covert a JSONObject into an XML text.
[中]这提供了将XML文本转换为JSONObject以及将JSONObject转换为XML文本的静态方法。
代码示例来源:origin: loklak/loklak_server
JSONObject json = XML.toJSONObject(content);
PrintWriter sos = response.getWriter();
JSONObject resourceObject = json.getJSONObject("resources");
JSONArray stringArray = resourceObject.getJSONArray("string-array");
JSONObject result = new JSONObject(true);
代码示例来源:origin: h2oai/h2o-2
public String toXml() {
JsonObject jo = this.toJson();
String jsonString = jo.toString();
org.json.JSONObject jo2 = new org.json.JSONObject(jsonString);
String xmlString = org.json.XML.toString(jo2);
return xmlString;
}
代码示例来源:origin: loklak/loklak_server
tagName = jo.optString("tagName");
if (tagName == null) {
return XML.escape(jo.toString());
XML.noSpace(tagName);
tagName = XML.escape(tagName);
sb.append('<');
sb.append(tagName);
for (final Entry<String, ?> entry : jo.entrySet()) {
final String key = entry.getKey();
if (!"tagName".equals(key) && !"childNodes".equals(key)) {
XML.noSpace(key);
value = entry.getValue();
if (value != null) {
sb.append(' ');
sb.append(XML.escape(key));
sb.append('=');
sb.append('"');
sb.append(XML.escape(value.toString()));
sb.append('"');
if (object != null) {
if (object instanceof String) {
sb.append(XML.escape(object.toString()));
} else if (object instanceof JSONObject) {
sb.append(toString((JSONObject)object));
代码示例来源:origin: loklak/loklak_server
XML.noSpace(tagName);
tagName = XML.escape(tagName);
sb.append('<');
sb.append(tagName);
for (final Entry<String, ?> entry : jo.entrySet()) {
final String key = entry.getKey();
XML.noSpace(key);
final Object value = entry.getValue();
if (value != null) {
sb.append(' ');
sb.append(XML.escape(key));
sb.append('=');
sb.append('"');
sb.append(XML.escape(value.toString()));
sb.append('"');
if (object != null) {
if (object instanceof String) {
sb.append(XML.escape(object.toString()));
} else if (object instanceof JSONObject) {
sb.append(toString((JSONObject)object));
代码示例来源:origin: loklak/loklak_server
for (final Entry<String, ?> entry : jo.entrySet()) {
final String key = entry.getKey();
Object value = entry.getValue();
sb.append('\n');
sb.append(escape(val.toString()));
i++;
sb.append(escape(value.toString()));
sb.append(key);
sb.append('>');
sb.append(toString(val));
sb.append("</");
sb.append(key);
sb.append('>');
} else {
sb.append(toString(val, key));
sb.append(toString(value, key));
sb.append(toString(val, tagName == null ? "array" : tagName));
string = (object == null) ? "null" : escape(object.toString());
return (tagName == null) ? "\"" + string + "\""
: (string.length() == 0) ? "<" + tagName + "/>" : "<" + tagName
代码示例来源:origin: loklak/loklak_server
newjo = new JSONObject();
if (arrayForm) {
newja.put(tagName);
newjo.put("tagName", tagName);
if (ja != null) {
ja.put(newjo);
throw x.syntaxError("Missing value");
newjo.accumulate(attribute, keepStrings ? ((String)token) :XML.stringToValue((String)token));
token = null;
} else {
if (ja != null) {
ja.put(token instanceof String
? keepStrings ? XML.unescape((String)token) :XML.stringToValue((String)token)
: token);
代码示例来源:origin: loklak/loklak_server
public static JSONObject fetchResults(String requestUrl, String operation) {
JSONObject itemlookup = new JSONObject(true);
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.transform(domSource, result);
JSONObject xmlresult = new JSONObject(true);
xmlresult = XML.toJSONObject(writer.toString());
JSONObject items = xmlresult.getJSONObject(operation).getJSONObject("Items");
if (items.getJSONObject("Request").has("Errors")) {
itemlookup.put("status", "error");
代码示例来源:origin: loklak/loklak_server
/**
* Convert a well-formed (but not necessarily valid) XML string into a
* JSONObject. Some information may be lost in this transformation because
* JSON is a data format and XML is a document format. XML uses elements,
* attributes, and content text, while JSON uses unordered collections of
* name/value pairs and arrays of values. JSON does not does not like to
* distinguish between elements and attributes. Sequences of similar
* elements are represented as JSONArrays. Content text may be placed in a
* "content" member. Comments, prologs, DTDs, and <code><[ [ ]]></code>
* are ignored.
*
* All values are converted as strings, for 1, 01, 29.0 will not be coerced to
* numbers but will instead be the exact value as seen in the XML document.
*
* @param string
* The source string.
* @param keepStrings If true, then values will not be coerced into boolean
* or numeric values and will instead be left as strings
* @return A JSONObject containing the structured data from the XML string.
* @throws JSONException Thrown if there is an errors while parsing the string
*/
public static JSONObject toJSONObject(String string, boolean keepStrings) throws JSONException {
JSONObject jo = new JSONObject();
XMLTokener x = new XMLTokener(string);
while (x.more() && x.skipPast("<")) {
parse(x, jo, null, keepStrings);
}
return jo;
}
/**
代码示例来源:origin: loklak/loklak_server
string = x.nextCDATA();
if (string.length() > 0) {
context.accumulate("content", string);
tagName = (String) token;
token = null;
jsonobject = new JSONObject();
for (;;) {
if (token == null) {
throw x.syntaxError("Missing value");
jsonobject.accumulate(string,
keepStrings ? ((String)token) : stringToValue((String) token));
token = null;
} else {
if (string.length() > 0) {
jsonobject.accumulate("content",
keepStrings ? string : stringToValue(string));
if (parse(x, jsonobject, tagName,keepStrings)) {
if (jsonobject.length() == 0) {
context.accumulate(tagName, "");
代码示例来源:origin: loklak/loklak_server
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Query post = RemoteAccess.evaluate(request);
// manage DoS
if (post.isDoS_blackout()) {response.sendError(503, "your request frequency is too high"); return;}
// evaluate get parameters
String data = post.get("data", "");
try {
String jsonData = XML.toJSONObject(data).toString();
JSONObject json = new JSONObject(jsonData);
PrintWriter sos = response.getWriter();
sos.print(json.toString(2));
sos.println();
}
catch (IOException e) {
DAO.severe(e);
JSONObject json = new JSONObject(true);
json.put("error", "Malformed XML. Please check XML Again");
json.put("type", "Error");
PrintWriter sos = response.getWriter();
sos.print(json.toString(2));
sos.println();
}
post.finalize();
}
代码示例来源:origin: com.xmlcalabash/xmlcalabash
tagName = jo.optString("tagName");
if (tagName == null) {
return XML.escape(jo.toString());
XML.noSpace(tagName);
tagName = XML.escape(tagName);
sb.append('<');
sb.append(tagName);
keys = jo.keys();
while (keys.hasNext()) {
k = keys.next();
if (!k.equals("tagName") && !k.equals("childNodes")) {
XML.noSpace(k);
v = jo.optString(k);
if (v != null) {
sb.append(' ');
sb.append(XML.escape(k));
sb.append('=');
sb.append('"');
sb.append(XML.escape(v));
sb.append('"');
if (e != null) {
if (e instanceof String) {
sb.append(XML.escape(e.toString()));
} else if (e instanceof JSONObject) {
sb.append(toString((JSONObject)e));
代码示例来源:origin: org.daisy.libs/com.xmlcalabash
XML.noSpace(tagName);
tagName = XML.escape(tagName);
sb.append('<');
sb.append(tagName);
keys = jo.keys();
while (keys.hasNext()) {
k = keys.next();
XML.noSpace(k);
v = jo.optString(k);
if (v != null) {
sb.append(' ');
sb.append(XML.escape(k));
sb.append('=');
sb.append('"');
sb.append(XML.escape(v));
sb.append('"');
if (e != null) {
if (e instanceof String) {
sb.append(XML.escape(e.toString()));
} else if (e instanceof JSONObject) {
sb.append(toString((JSONObject)e));
代码示例来源:origin: b3log/latke
tagName = jo.optString("tagName");
if (tagName == null) {
return XML.escape(jo.toString());
XML.noSpace(tagName);
tagName = XML.escape(tagName);
sb.append('<');
sb.append(tagName);
for (final String key : jo.keySet()) {
if (!"tagName".equals(key) && !"childNodes".equals(key)) {
XML.noSpace(key);
value = jo.opt(key);
if (value != null) {
sb.append(' ');
sb.append(XML.escape(key));
sb.append('=');
sb.append('"');
sb.append(XML.escape(value.toString()));
sb.append('"');
if (object != null) {
if (object instanceof String) {
sb.append(XML.escape(object.toString()));
} else if (object instanceof JSONObject) {
sb.append(toString((JSONObject)object));
代码示例来源:origin: amzn/amazon-pay-sdk-java
/**
* Converts the raw XML response to JSON format
*
* @throws JSONException
* If the XML response cannot be converted into JSON
* @return
* JSON representation of response returned by Amazon Service API
*/
public String toJSON() throws JSONException {
JSONObject jsonObj = XML.toJSONObject(this.responseXml);
return jsonObj.toString();
}
代码示例来源:origin: com.vaadin/vaadin-shared-deps
keys = jo.keys();
while (keys.hasNext()) {
k = keys.next().toString();
v = jo.get(k).toString();
b.append(' ');
b.append(k);
b.append("=\"");
b.append(XML.escape((String)v));
b.append('"');
stringify((JSONArray)v, b);
} else {
b.append(XML.escape(v.toString()));
代码示例来源:origin: tmobile/pacbot
/**
* @param response
* @return
*/
private boolean sniffPublicAccess(String response) {
JSONObject jsonObject = XML.toJSONObject(response);
return "NoSuchKey".equals(jsonObject.getJSONObject("Error").get("Code"));
}
代码示例来源:origin: b3log/latke
XML.noSpace(tagName);
tagName = XML.escape(tagName);
sb.append('<');
sb.append(tagName);
for (final String key : jo.keySet()) {
final Object value = jo.opt(key);
XML.noSpace(key);
if (value != null) {
sb.append(' ');
sb.append(XML.escape(key));
sb.append('=');
sb.append('"');
sb.append(XML.escape(value.toString()));
sb.append('"');
if (object != null) {
if (object instanceof String) {
sb.append(XML.escape(object.toString()));
} else if (object instanceof JSONObject) {
sb.append(toString((JSONObject)object));
代码示例来源:origin: b3log/latke
for (final String key : jo.keySet()) {
Object value = jo.opt(key);
if (value == null) {
value = "";
sb.append(escape(val.toString()));
sb.append(escape(value.toString()));
sb.append(key);
sb.append('>');
sb.append(toString(val));
sb.append("</");
sb.append(key);
sb.append('>');
} else {
sb.append(toString(val, key));
sb.append(toString(value, key));
sb.append(toString(val, tagName == null ? "array" : tagName));
string = (object == null) ? "null" : escape(object.toString());
return (tagName == null) ? "\"" + string + "\""
: (string.length() == 0) ? "<" + tagName + "/>" : "<" + tagName
代码示例来源:origin: loklak/loklak_server
/**
* Convert a JSONObject into a well-formed, element-normal XML string.
*
* @param object
* A JSONObject.
* @return A string.
* @throws JSONException Thrown if there is an error parsing the string
*/
public static String toString(Object object) throws JSONException {
return toString(object, null);
}
代码示例来源:origin: loklak/loklak_server
/**
* Convert a well-formed (but not necessarily valid) XML string into a
* JSONObject. Some information may be lost in this transformation because
* JSON is a data format and XML is a document format. XML uses elements,
* attributes, and content text, while JSON uses unordered collections of
* name/value pairs and arrays of values. JSON does not does not like to
* distinguish between elements and attributes. Sequences of similar
* elements are represented as JSONArrays. Content text may be placed in a
* "content" member. Comments, prologs, DTDs, and <code><[ [ ]]></code>
* are ignored.
*
* @param string
* The source string.
* @return A JSONObject containing the structured data from the XML string.
* @throws JSONException Thrown if there is an errors while parsing the string
*/
public static JSONObject toJSONObject(String string) throws JSONException {
return toJSONObject(string, false);
}
内容来源于网络,如有侵权,请联系作者删除!