我是 JAXB 的新手,在从 XML 到 Java 类实例的转换方面遇到了麻烦。
我有以下 XML:
<?xml version="1.0"?>
<response>
<category>client</category>
<action>Greeting</action>
<code>1000</code>
<msg>Your Connection with API Server is Successful</msg>
<resData>
<data name="svDate">2009-02-16 06:22:21</data>
</resData>
</response>
我开发了以下 Java 代码:
/**
* Copyright 2013. ABN Software. All Rights reserved.<br>
* Author ...... Andre<br>
* Created ..... 14.03.2013<br>
* <br>
*/
package net.regmaster.onlinenic.model;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementRef;
import javax.xml.bind.annotation.XmlRootElement;
import net.regmaster.onlinenic.enumtype.OnicEnumAction;
import net.regmaster.onlinenic.enumtype.OnicEnumCategory;
import net.regmaster.onlinenic.model.resdata.GreetingResData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author annik
*
*/
@XmlRootElement(name = "response")
// @XmlType( propOrder = { "category", "action", "code", "message"})
public class OnicGreeting
{
private OnicEnumCategory category;
private OnicEnumAction action;
private Integer code;
private String message;
private GreetingResData resData;
//
private Logger LOG = LoggerFactory.getLogger(getClass());
/**
* Getter.
*
* @return the category
*/
public OnicEnumCategory getCategory() {
return category;
}
/**
* Setter.
*
* @param category
* the category to set
*/
public void setCategoryEnum(OnicEnumCategory category) {
this.category = category;
}
@XmlElement
public void setCategory(String category) {
try {
this.category = OnicEnumCategory.getEnum(category);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
LOG.error(e.getMessage());
}
}
/**
* Getter.
*
* @return the action
*/
public OnicEnumAction getAction() {
return action;
}
/**
* Setter.
*
* @param action
* the action to set
*/
public void setActionEnum(OnicEnumAction action) {
this.action = action;
}
@XmlElement
public void setAction(String action) {
try {
this.action = OnicEnumAction.getEnum(action);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
LOG.error(e.getMessage());
}
}
/**
* Getter.
*
* @return the code
*/
@XmlElement
public Integer getCode() {
return code;
}
/**
* Setter.
*
* @param code
* the code to set
*/
public void setCode(Integer code) {
this.code = code;
}
/**
* Getter.
*
* @return the message
*/
@XmlElement(name = "msg")
public String getMessage() {
return message;
}
/**
* Setter.
*
* @param message
* the message to set
*/
public void setMessage(String message) {
this.message = message;
}
/**
* Getter.
*
* @return the resData
*/
@XmlElementRef
public GreetingResData getResData() {
return resData;
}
/**
* Setter.
*
* @param resData
* the resData to set
*/
public void setResData(GreetingResData resData) {
this.resData = resData;
}
@Override
public String toString() {
return "category=" + category + ", action=" + action + ", code=" + code + ", msg=" + message
+ ", resData:" + resData.toString();
}
}
和
/**
* Copyright 2013. ABN Software. All Rights reserved.<br>
* Author ...... Andre<br>
* Created ..... 14.03.2013<br>
* <br>
*/
package net.regmaster.onlinenic.model.resdata;
import java.util.Calendar;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.XmlValue;
/**
* @author annik
*
*/
@XmlRootElement(name="resData")
public class GreetingResData extends AbstractResData
{
String svDate;
/**
* Constructor.
*
*/
public GreetingResData() {
// TODO Auto-generated constructor stub
}
/**
* Getter.
*
* @return the svDate
*/
@XmlAttribute
public String getSvDate() {
return svDate;
}
/**
* Setter.
*
* @param svDate
* the svDate to set
*/
public void setSvDate(String svDate) {
this.svDate = svDate;
}
}
这些代码示例运行但数据错误:
http://i.stack.imgur.com/qCCIM.png
请帮帮我。
我也不明白万一我会有很多不同的
<data ...>..</data>
我可以轻松简单地做些什么?
我的意思是这种情况:
<resData>
<data name="crDate">2004-12-17</data>
<data name="exDate">2009-01-02</data>
<data name="password">7fe11fd9d97ee40bdf57e561427c0a6</data>
<data name="dns">dns1.onlinenic.net</data>
<data name="dns">dns2.onlinenic.net</data>
<data name="r_name">123456</data>
<data name="r_org">123456</data>
<data name="r_country">BJ</data>
<data name="r_province">mokcup</data>
<data name="r_city">123456</data>
<data name="r_street">123456</data>
<data name="r_postalcode">123456</data>
<data name="r_voice">+86.5925391800</data>
<data name="r_fax">+86.5925391800</data>
<data name="r_email">asdfasdf@sadf.com</data>
....
最佳答案
谢谢 Blaise Doughan。 但是在挖掘了 10 多个主题之后,我决定我必须从相反的方式开始。
我创建了新的测试,用于整理我的数据(对象)。实际上,我使用了我认为的 TDD(测试驱动开发)方式。 因此,我用测试数据填充我的对象并应用编码(从数据创建 XML)并看到我得到了。数据不正确。我也查看了其他主题(感谢这个 Java/JAXB: Unmarshall Xml to specific subclass based on an attribute )并更正了我的数据结构
记住我喜欢得到
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<response>
<code>1000</code>
<message>Big message</message>
<resData>
<data name="svDate">2013.03.14</data>
</resData>
</response>
现在我的数据是:
package net.regmaster.onlinenic.model.response.resdata;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlValue;
import org.eclipse.persistence.oxm.annotations.XmlCustomizer;
/**
* @author annik
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name="data")
//@XmlCustomizer(ResDataCustomiser.class)
public class XmlData
{
@XmlAttribute(name="name")
private String name;
@XmlValue
private String value;
/** Getter.
* @return the name
*/
public String getName() {
return name;
}
/** Setter.
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/** Getter.
* @return the value
*/
public String getValue() {
return value;
}
/** Setter.
* @param value the value to set
*/
public void setValue(String value) {
this.value = value;
}
}
和:
package net.regmaster.onlinenic.model.response.resdata;
import java.util.List;
import javax.xml.bind.annotation.XmlRootElement;
/**
* @author annik
*
*/
@XmlRootElement
public class ResData
{
private List<XmlData> data;
/**
* Getter.
*
* @return the data
*/
public List<XmlData> getData() {
return data;
}
/**
* Setter.
*
* @param data
* the data to set
*/
public void setData(List<XmlData> data) {
this.data = data;
}
}
和:
package net.regmaster.onlinenic.model.response;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElements;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
import javax.xml.bind.annotation.XmlType;
import net.regmaster.onlinenic.enumtype.OnicEnumAction;
import net.regmaster.onlinenic.enumtype.OnicEnumCategory;
import net.regmaster.onlinenic.model.response.resdata.ResData;
import net.regmaster.onlinenic.model.response.resdata.XmlData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author annik
*
*/
@XmlRootElement(name = "response")
//@XmlType( propOrder = { "category", "action", "code", "message"})
public class OnicGreetingResponse
{
private OnicEnumCategory category;
private OnicEnumAction action;
private Integer code;
private String message;
// private GreetingResData resData;
private ResData resData;
//
@XmlTransient
private Logger LOG = LoggerFactory.getLogger(getClass());
/**
* Getter.
*
* @return the category
*/
public OnicEnumCategory getCategory() {
return category;
}
/**
* Setter.
*
* @param category
* the category to set
*/
public void setCategoryEnum(OnicEnumCategory category) {
this.category = category;
}
@XmlElement
public void setCategory(String category) {
try {
this.category = OnicEnumCategory.getEnum(category);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
LOG.error(e.getMessage());
}
}
/**
* Getter.
*
* @return the action
*/
public OnicEnumAction getAction() {
return action;
}
/**
* Setter.
*
* @param action
* the action to set
*/
public void setActionEnum(OnicEnumAction action) {
this.action = action;
}
@XmlElement
public void setAction(String action) {
try {
this.action = OnicEnumAction.getEnum(action);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
LOG.error(e.getMessage());
}
}
/**
* Getter.
*
* @return the code
*/
@XmlElement
public Integer getCode() {
return code;
}
/**
* Setter.
*
* @param code
* the code to set
*/
public void setCode(Integer code) {
this.code = code;
}
/**
* Getter.
*
* @return the message
*/
@XmlElements(value={@XmlElement})
public String getMessage() {
return message;
}
/**
* Setter.
*
* @param message
* the message to set
*/
public void setMessage(String message) {
this.message = message;
}
/** Getter.
* @return the resData
*/
public ResData getResData() {
return resData;
}
/** Setter.
* @param resData the resData to set
*/
@XmlElement
public void setResData(ResData resData) {
this.resData = resData;
}
@Override
public String toString() {
return "category=" + category + ", action=" + action + ", code=" + code + ", msg=" + message
+ ", resData:" + resData.toString();
}
}
和 vu-alja : 我明白了!
正如您在下面看到的,它以另一种方式工作: http://i.stack.imgur.com/35nzb.png
关于java - JAXB @XmlAttribute @XmlValue 实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15415136/
我正在查看instance_variable_set的文档并看到给出的示例代码是这样做的:obj.instance_variable_set(:@instnc_var,"valuefortheinstancevariable")然后允许您在类的任何实例方法中以@instnc_var的形式访问该变量。我想知道为什么在@instnc_var之前需要一个冒号:。冒号有什么作用? 最佳答案 我的第一直觉是告诉你不要使用instance_variable_set除非你真的知道你用它做什么。它本质上是一种元编程工具或绕过实例变量可见性的黑客攻击
在我的应用程序中,我需要能够找到所有数字子字符串,然后扫描每个子字符串,找到第一个匹配范围(例如5到15之间)的子字符串,并将该实例替换为另一个字符串“X”。我的测试字符串s="1foo100bar10gee1"我的初始模式是1个或多个数字的任何字符串,例如,re=Regexp.new(/\d+/)matches=s.scan(re)给出["1","100","10","1"]如果我想用“X”替换第N个匹配项,并且只替换第N个匹配项,我该怎么做?例如,如果我想替换第三个匹配项“10”(匹配项[2]),我不能只说s[matches[2]]="X"因为它做了两次替换“1fooX0barXg
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
我有一个正在构建的应用程序,我需要一个模型来创建另一个模型的实例。我希望每辆车都有4个轮胎。汽车模型classCar轮胎模型classTire但是,在make_tires内部有一个错误,如果我为Tire尝试它,则没有用于创建或新建的activerecord方法。当我检查轮胎时,它没有这些方法。我该如何补救?错误是这样的:未定义的方法'create'forActiveRecord::AttributeMethods::Serialization::Tire::Module我测试了两个环境:测试和开发,它们都因相同的错误而失败。 最佳答案
我正在处理旧代码的一部分。beforedoallow_any_instance_of(SportRateManager).toreceive(:create).and_return(true)endRubocop错误如下:Avoidstubbingusing'allow_any_instance_of'我读到了RuboCop::RSpec:AnyInstance我试着像下面那样改变它。由此beforedoallow_any_instance_of(SportRateManager).toreceive(:create).and_return(true)end对此:let(:sport_
我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www
我收到格式为的回复#我需要将其转换为哈希值(针对活跃商家)。目前我正在遍历变量并执行此操作:response.instance_variables.eachdo|r|my_hash.merge!(r.to_s.delete("@").intern=>response.instance_eval(r.to_s.delete("@")))end这有效,它将生成{:first="charlie",:last=>"kelly"},但它似乎有点hacky和不稳定。有更好的方法吗?编辑:我刚刚意识到我可以使用instance_variable_get作为该等式的第二部分,但这仍然是主要问题。
我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我
什么是ruby的rack或python的Java的wsgi?还有一个路由库。 最佳答案 来自Python标准PEP333:Bycontrast,althoughJavahasjustasmanywebapplicationframeworksavailable,Java's"servlet"APImakesitpossibleforapplicationswrittenwithanyJavawebapplicationframeworktoruninanywebserverthatsupportstheservletAPI.ht
这篇文章是继上一篇文章“Observability:从零开始创建Java微服务并监控它(一)”的续篇。在上一篇文章中,我们讲述了如何创建一个Javaweb应用,并使用Filebeat来收集应用所生成的日志。在今天的文章中,我来详述如何收集应用的指标,使用APM来监控应用并监督web服务的在线情况。源码可以在地址 https://github.com/liu-xiao-guo/java_observability 进行下载。摄入指标指标被视为可以随时更改的时间点值。当前请求的数量可以改变任何毫秒。你可能有1000个请求的峰值,然后一切都回到一个请求。这也意味着这些指标可能不准确,你还想提取最小/