I have used a .xsd file to generate Java classes, and with an XML file, I need to unmarshall.
I am using this code :
JAXBContext objJAXBContext = JAXBContext.newInstance("my.test");
// create an Unmarshaller
Unmarshaller objUnmarshaller = objJAXBContext.createUnmarshaller();
FileInputStream fis = new FileInputStream("test.xml");
JAXBElement<Root> objMyRoot = (JAXBElement<Root>) objUnmarshaller.unmarshal(fis);
Root mRoot = objMyRoot.getValue();
and I am getting this error:
javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"Root"). Expected elements are (none)
I have seen many solutions but nothing works in my project.
What i can try to do?
Your xml root is missing the namespace (uri) attribute.
You better try this on the XMLRootElement ...
#XmlRootElement(name = "root", namespace="")
Try
StreamSource streamSource = new StreamSource("test.xml")
JAXBElement<Root> objMyRoot = (JAXBElement<Root>) objUnmarshaller.unmarshal(streamsource, Root.class);
Related
Any idea how I can add my output stream to the build config?
ConfigurationBuilder<BuiltConfiguration> builder =
ConfigurationBuilderFactory.newConfigurationBuilder();
AppenderComponentBuilder osAppender = builder.newAppender("os", "OutputStream");
osAppender.addAttribute("target", myStream);
builder.add(osAppender);
BuiltConfiguration config = builder.build();
Configurator.initialize(config);
This is the Error message I get:
2022-01-27 15:04:41,203 main ERROR OutputStream contains an invalid element or attribute "target"
2022-01-27 15:04:41,227 main ERROR Could not create plugin of type class org.apache.logging.log4j.core.appender.OutputStreamAppender for element OutputStream: java.lang.NullPointerException java.lang.NullPointerException
at org.apache.logging.log4j.core.appender.OutputStreamAppender.getManager(OutputStreamAppender.java:159)
Thanks
The ConfigurationBuilder API does not allow you to set attributes which can not be serialized to a String. Therefore you'll need to use OutputSreamAppender's builder directly:
final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
final Configuration config = ctx.getConfiguration();
final Appender appender = OutputStreamAppender.newBuilder()//
.setTarget(myStream)
.setConfiguration(ctx.getConfiguration())
.build();
config.addLoggerAppender(ctx.getRootLogger(), appender);
See this question for another example of ConfigurationBuilder API vs direct instantiation of Log4j components.
Check also Log4j's architecture, which explains how all these components work together.
I have added HTML2XHTML as a nuget package to my project.
However I get "command not found" exception when I try to use
using Corsis.Xhtml;
//input is an html string
var xhtml = Html2Xhtml.RunAsFilter(stdin => stdin.Write(input)).ReadToEnd();
Can anyone please help me to fix this.
I know it's not on the subject but this is related and may solve your problem.
Try this please :
install SgmlReader from nuget
in case you have a string variable like below you will have to convert it into a TextReader object.
Now we are going to use the package installed.
static XmlDocument HTMLTEST()
{
string html = "<table frame=all><tgroup></tgroup></table>";
TextReader reader = new StringReader(html);
Sgml.SgmlReader sgmlReader = new Sgml.SgmlReader();
sgmlReader.DocType = "HTML";
sgmlReader.WhitespaceHandling = System.Xml.WhitespaceHandling.All;
sgmlReader.InputStream = reader;
XmlDocument doc = new XmlDocument();
doc.PreserveWhitespace = true; //false if you dont want whitespace
doc.XmlResolver = null;
doc.Load(sgmlReader);
return doc;
}
Input is string html format, and the return will be doc XmlDocument format. Your frame=all from html will become frame="all".
I can do whatever you like with the proper formatted XmlDocument doc, make it Xhtml if that's whant you wanted.
I'm using a Restful web service (Jersy implementation) with a JSF application and used Json to get the data as follows:
carObjectDao = new GenericDAO<carObject>(carObject.class);
List<carObject> allCars = carObjectDao.readAll();
Gson gson = new Gson();
String carString = gson.toJson(allCars);
System.err.println(carString );
return carString ;
i run the application in debug mode and allCars is filled with the data correctly, but after that an exception is thrown :
java.lang.UnsupportedOperationException: Attempted to serialize java.lang.Class: org.hibernate.proxy.HibernateProxy. Forgot to register a type adapter?
i don't know the root cause of the exception
This is a known problem: Could not serialize object cause of HibernateProxy
JSon can't deserialize HibernateProxy objects, so you either unproxy or remove em.
Or, you can eager fetch the lazy data.
Try parsing through ObjectMapper as
carObjectDao = new GenericDAO<carObject>(carObject.class);
List<carObject> allCars = carObjectDao.readAll();
String carString = new ObjectMapper().writeValueAsString(allCars);
System.err.println(carString );
return carString ;
I was added "jackson-mini-1.9.2.jar"(is not "jackson-all-1.9.2.jar") in my project,
I want to convert json to object class.
Use "jackson-all-1.9.2.jar",we can use "ObjectMapper" to get it.
but use "jackson-mini-1.9.2.jar",How to do it?
If I write the follow code"
String json = "{\"name\" : {\"first\" : \"Joe\", \"last\" : \"Sixpack\" }, \"gender\" : \"MALE\", \"verified\" : false, \"userImage\" : \"Rm9vYmFyIQ==\" }";
JsonFactory f = new JsonFactory();
JsonParser jp = f.createJsonParser(json);
User user = jp.readValueAs(User.class);
The result is like that:
Exception in thread "main" java.lang.IllegalStateException: No ObjectCodec defined for the parser, can not deserialize JSON into Java objects
at org.codehaus.jackson.JsonParser.readValueAs(Unknown Source)
at TestJackson.main(TestJackson.java:21)
You can implement your own ObjectCodec and then register it with the JsonFactory by calling JsonFactory#setCodec(myCodec).
Or (much easier!), just get hold of jackson-mapper-1.9.2.jar and add it to your classpath, so that you can use the default ObjectMapper.
If you want to use data-binding, do NOT use mini jar. It is only meant as the smallest possible jar to use Streaming Parsing (JsonParser, JsonGenerator).
[{"ID":"hzQ8ll","CreationDate":"Thu, 24 Feb 2011 12:53:31 GMT","Count":6,"Name":"SOMETAG"}]
The inside is of type Tag so I just wrote this Java class:
public class Tags {
public List <Tag>tags;
}
But I get com.sun.jersey.api.client.ClientHandlerException:
org.codehaus.jackson.map.JsonMappingException: Can not deserialize instance of com.test.Tags out of START_ARRAY token
I am using Jersey with the JacksonJsonProvider like this:
ClientConfig config = new DefaultClientConfig();
config.getClasses().add(JacksonJsonProvider.class);
Then I just do a simple Jersey client call:
ClientResponse response = builder.get(ClientResponse.class);
Tags tags = response.getEntity(Tags.class);
Any ideas? Most of the time my outermost elements had a name associated to it so this is new to me. Thanks for any help
You possibly have to declare a Tag[] instead of a List<Tag>.
I had a similar issue with a different JSON library.
It seems to have to do with difficulties introspecting generic containers.
You have a strange usage of get().
http://jersey.java.net/nonav/apidocs/1.5/jersey/com/sun/jersey/api/client/UniformInterface.html#get%28java.lang.Class%29
Return and argument type should be the same.
Either:
ClientResponse resp = builder.get(ClientResponse.class);
or
Tag[] resp = builder.get(Tag[].class);
Anyway, it seems tha the problem is that your JSON data is an array and it is being deserialized into something that is not (Tags).
Try this directly:
Tag[] tags = response.getEntity(Tag[].class);