Comments (15)
You may use U.format(string, variable)
as an alternative.
Code:
String response = "<FinalResponse>\r\n" +
" <Type>SUCCESS</Type>\r\n" +
" <Code>0</Code>\r\n" +
"</FinalResponse>";
String xml ="<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"
+ " <soapenv:Header/>"
+ " <soapenv:Body>"
+ "<getMyData xmlns:ns10=\"urn:mybay.com:dms:wsdls:Visit\" xmlns:ns5=\"urn:mybay.com:enterprise:schemas:common:elements\" xmlns:ns6=\"urn:mybay.com:enterprise:schemas:Visit\" xmlns:ns7=\"urn:mybay.com:dms:schemas:Visit\" xmlns:ns8=\"urn:mybay.com:enterprise:schemas:account\">"
+ "<Visit>"
+ "<item></item>"
+ "</Visit>"
+ "{0}"
+ "</getMyData>"
+ "</soapenv:Body>"
+ "</soapenv:Envelope>";
Map<String, Object> map = (Map<String, Object>) U.fromXml(U.format(xml, response));
String json = "[{\"id\":\"1\",\"name\":\"Bratislava\",\"population\":\"432000\"},{\"id\":\"2\",\"name\":\"Budapest\",\"population\":\"1759000\"},{\"id\":\"3\",\"name\":\"Prague\",\"population\":\"1280000\"},{\"id\":\"4\",\"name\":\"Warsaw\",\"population\":\"1748000\"},{\"id\":\"5\",\"name\":\"Los Angeles\",\"population\":\"3971000\"},{\"id\":\"6\",\"name\":\"New York\",\"population\":\"8550000\"},{\"id\":\"7\",\"name\":\"Edinburgh\",\"population\":\"464000\"},{\"id\":\"8\",\"name\":\"Berlin\",\"population\":\"3671000\"}]";
List<Object> list = (List<Object>) U.fromJson(json);
U.set(map, "soapenv:Envelope.soapenv:Body.getMyData.Visit.item", list);
System.out.println(U.toXml(map));
from underscore-java.
You also can add your response after Visit section.
String response = "<FinalResponse>\r\n" +
" <Type>SUCCESS</Type>\r\n" +
" <Code>0</Code>\r\n" +
"</FinalResponse>";
String xml ="<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"
+ " <soapenv:Header/>"
+ " <soapenv:Body>"
+ "<getMyData xmlns:ns10=\"urn:mybay.com:dms:wsdls:Visit\" xmlns:ns5=\"urn:mybay.com:enterprise:schemas:common:elements\" xmlns:ns6=\"urn:mybay.com:enterprise:schemas:Visit\" xmlns:ns7=\"urn:mybay.com:dms:schemas:Visit\" xmlns:ns8=\"urn:mybay.com:enterprise:schemas:account\">"
+ "<Visit>"
+ "<item></item>"
+ "</Visit>"
+ "</getMyData>"
+ "</soapenv:Body>"
+ "</soapenv:Envelope>";
Map<String, Object> map = (Map<String, Object>) U.fromXml(xml);
Map<String, Object> responsemap = (Map<String, Object>) U.fromXml(response);
String json = "[{\"id\":\"1\",\"name\":\"Bratislava\",\"population\":\"432000\"},{\"id\":\"2\",\"name\":\"Budapest\",\"population\":\"1759000\"},{\"id\":\"3\",\"name\":\"Prague\",\"population\":\"1280000\"},{\"id\":\"4\",\"name\":\"Warsaw\",\"population\":\"1748000\"},{\"id\":\"5\",\"name\":\"Los Angeles\",\"population\":\"3971000\"},{\"id\":\"6\",\"name\":\"New York\",\"population\":\"8550000\"},{\"id\":\"7\",\"name\":\"Edinburgh\",\"population\":\"464000\"},{\"id\":\"8\",\"name\":\"Berlin\",\"population\":\"3671000\"}]";
List<Object> list = (List<Object>) U.fromJson(json);
U.set(map, "soapenv:Envelope.soapenv:Body.getMyData.Visit.item", list);
Map<String, Object> getMyData = (Map<String, Object>) U.get(map, "soapenv:Envelope.soapenv:Body.getMyData");
getMyData.put(responsemap.entrySet().iterator().next().getKey(), responsemap.entrySet().iterator().next().getValue());
System.out.println(U.toXml(map));
from underscore-java.
Great Solution. Look like this library is matured to use any usecase. The first one looks better when you wants to attached multiple bits and pieces
from underscore-java.
This doesn't works when want to replace multiple bits and pieces.
Error
Exception in thread "main" java.lang.IllegalArgumentException: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 273; The content of elements must consist of well-formed character data or markup.
at com.github.underscore.lodash.Xml.fromXml(Xml.java:1322)
at com.github.underscore.lodash.Xml.fromXml(Xml.java:1293)
at com.github.underscore.lodash.U.fromXml(U.java:1995)
at com.example.MyDev.main(MyDev.java:30)
Caused by: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 273; The content of elements must consist of well-formed character data or markup.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:203)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.fatalError(ErrorHandlerWrapper.java:177)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:400)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:327)
at com.sun.org.apache.xerces.internal.impl.XMLScanner.reportFatalError(XMLScanner.java:1472)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.startOfMarkup(XMLDocumentFragmentScannerImpl.java:2635)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2732)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:602)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:112)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:505)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:842)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:771)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:243)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:339)
at com.github.underscore.lodash.Xml$Document.createDocument(Xml.java:1402)
at com.github.underscore.lodash.Xml$Document.access$400(Xml.java:1388)
at com.github.underscore.lodash.Xml.fromXml(Xml.java:1306)
... 3 more
String response = "<FinalResponse>\r\n" +
" <Type>SUCCESS</Type>\r\n" +
" <Code>0</Code>\r\n" +
"</FinalResponse>";
String xmlns = "<getMyData xmlns:ns10=\"urn:mybay.com:dms:wsdls:Visit\" xmlns:ns5=\"urn:mybay.com:enterprise:schemas:common:elements\" xmlns:ns6=\"urn:mybay.com:enterprise:schemas:Visit\" xmlns:ns7=\"urn:mybay.com:dms:schemas:Visit\" xmlns:ns8=\"urn:mybay.com:enterprise:schemas:account\">";
String xml ="<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"
+ " <soapenv:Header/>"
+ " <soapenv:Body>"
+ " {1}"
+ "<Visit>"
+ "<item></item>"
+ "</Visit>"
+ "{0}"
+ "</getMyData>"
+ "</soapenv:Body>"
+ "</soapenv:Envelope>";
Map<String, Object> map = (Map<String, Object>) U.fromXml(U.format(xml, response));
String json = "[{\"id\":\"1\",\"name\":\"Bratislava\",\"population\":\"432000\"},{\"id\":\"2\",\"name\":\"Budapest\",\"population\":\"1759000\"},{\"id\":\"3\",\"name\":\"Prague\",\"population\":\"1280000\"},{\"id\":\"4\",\"name\":\"Warsaw\",\"population\":\"1748000\"},{\"id\":\"5\",\"name\":\"Los Angeles\",\"population\":\"3971000\"},{\"id\":\"6\",\"name\":\"New York\",\"population\":\"8550000\"},{\"id\":\"7\",\"name\":\"Edinburgh\",\"population\":\"464000\"},{\"id\":\"8\",\"name\":\"Berlin\",\"population\":\"3671000\"}]";
List<Object> list = (List<Object>) U.fromJson(json);
U.set(map, "soapenv:Envelope.soapenv:Body.getMyData.Visit.item", list);
System.out.println(U.toXml(map));
from underscore-java.
I checked this solution:
String response = "<FinalResponse>\r\n" +
" <Type>SUCCESS</Type>\r\n" +
" <Code>0</Code>\r\n" +
"</FinalResponse>";
String xmlns = "<getMyData xmlns:ns10=\"urn:mybay.com:dms:wsdls:Visit\" xmlns:ns5=\"urn:mybay.com:enterprise:schemas:common:elements\" xmlns:ns6=\"urn:mybay.com:enterprise:schemas:Visit\" xmlns:ns7=\"urn:mybay.com:dms:schemas:Visit\" xmlns:ns8=\"urn:mybay.com:enterprise:schemas:account\">"
+ "<Visit>"
+ "<item></item>"
+ "</Visit>"
+ "</getMyData>";
String xml ="<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"
+ " <soapenv:Header/>"
+ " <soapenv:Body>"
+ " {0}"
+ "{1}"
+ "</soapenv:Body>"
+ "</soapenv:Envelope>";
Map<String, Object> map = (Map<String, Object>) U.fromXml(U.format(xml, xmlns, response));
String json = "[{\"id\":\"1\",\"name\":\"Bratislava\",\"population\":\"432000\"},{\"id\":\"2\",\"name\":\"Budapest\",\"population\":\"1759000\"},{\"id\":\"3\",\"name\":\"Prague\",\"population\":\"1280000\"},{\"id\":\"4\",\"name\":\"Warsaw\",\"population\":\"1748000\"},{\"id\":\"5\",\"name\":\"Los Angeles\",\"population\":\"3971000\"},{\"id\":\"6\",\"name\":\"New York\",\"population\":\"8550000\"},{\"id\":\"7\",\"name\":\"Edinburgh\",\"population\":\"464000\"},{\"id\":\"8\",\"name\":\"Berlin\",\"population\":\"3671000\"}]";
List<Object> list = (List<Object>) U.fromJson(json);
U.set(map, "soapenv:Envelope.soapenv:Body.getMyData.Visit.item", list);
System.out.println(U.toXml(map));
from underscore-java.
Thank you. In short, we can not break the syntax, the complete piece/tag has move out to make this working. I am not sure on flexibility.
from underscore-java.
It is better to add the XML section to a template.
from underscore-java.
Is there any way if we can convert Complex POJO to XML or JSON?
from underscore-java.
We may convert Pojo to map first.
from underscore-java.
Could you please let me know which method I can use ?
from underscore-java.
To convert map to xml you may use U.toXml(map) method.
from underscore-java.
Yeah, I was able to convert POJO to map using ObjectMapper API, is there any way if we can do using underscore library ? Will you please also guide on other query I raised ?
from underscore-java.
Underscore-java only can convert string to map and vice versa.
from underscore-java.
Thanks !
Look like below code has issue and its not replacing the values:
Map<String, Object> objectMap = mapper.convertValue(response, Map.class);
String soapHeader = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><soapenv:Header/><soapenv:Body>{0}</soapenv:Body></soapenv:Envelope>";
String successResponse = "<OperationResponse><Type>SUCCESS</Type><Code>0</Code><ResponseDetails><ResponseDetail><Message>Operation completed successfully</Message></ResponseDetail></ResponseDetails></OperationResponse>";
Map<String, Object> map = (Map<String, Object>) U.fromXml(U.format(soapHeader, response));
U.set(map, "soapenv:Envelope.soapenv:Body", objectMap);
System.out.println(U.toXml(map));
from underscore-java.
You may check a simple map with only one value.
from underscore-java.
Related Issues (20)
- xmlToJson: array with size = 1 is written as object HOT 1
- How to get the deeply nested xml elements while converting from xml to map? HOT 4
- Remove string="true" attribute while converting json to xml HOT 3
- Convert xml to json HOT 9
- Build json with null value HOT 4
- How to create node with value and attribute? HOT 1
- Retaining the mandatory <root> during jsontoxml conversion HOT 7
- Is there a get for nested hetrogenus maps? HOT 3
- xml nil="true" support HOT 5
- How to make json string to beautiful json format with color HOT 7
- Is the source for xmltojson.jar available? HOT 3
- Site pointing to 1.77 HOT 2
- JSON serialization should quote enum members HOT 1
- Add Base32 RFC 4648 alphabet HOT 2
- How could i use it in Java 8 project? HOT 2
- Question: XML to JSON attribute prefix HOT 3
- Handling Indentation HOT 3
- 请问有支持JDK1.7 和 JDK8 的jar包可以直接用吗? HOT 1
- JSON to XML conversion heap optimization HOT 3
- Ignore colon (:) in root element if specified. HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from underscore-java.