[英]xml unmarshalling and comparing to java files
我正在尝试制作可以比较xml和客户端插入值的值的程序。 程序应该像字典一样工作,仅适用于xml中定义的单词。 这是我的代码:
package rest;
import java.io.File;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.jws.WebService;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
@WebService(endpointInterface = "rest.Translator")
public class TranslatorImp implements Translator{
@Override
public String translate(String rec, String jezik1, String jezik2) {
for (Word word : words) {
if(rec.equals(word.getEnglish()) && "english".equals(jezik1) && "serbian".equals(jezik2)){
return word.getSerbian();
}else
if(rec.equals(word.getSerbian()) && "serbian".equals((String)jezik1) && "english".equals((String)jezik2)){
return word.getEnglish();
}
}
return "Sorry, something went wrong. Please try again..";
}
}
这是我的界面:
package rest;
import java.io.File;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
@WebService
public interface Translator {
@WebMethod
String translate(String rec, String jezik1, String jezik2);
}
这是我完成编组的主要课程:
package rest;
import java.io.File;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
public class Main {
public static void main(String[] args) throws JAXBException{
JAXBContext jaxbc = JAXBContext.newInstance("word");
Unmarshaller unmarshaller = jaxbc.createUnmarshaller();
Root root = (Root)unmarshaller.unmarshal(new File("word.xml"));
List<Word> words = root.getWord();
}
}
这是我的xml:
<?xml version="1.0" encoding="UTF-8" ?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="Word.xsd">
<word>
<english>word</english>
<serbian>rec</serbian>
</word>
<word>
<english>butterfly</english>
<serbian>leptir</serbian>
</word>
<word>
<english>house</english>
<serbian>kuca</serbian>
</word>
<word>
<english>computer</english>
<serbian>kompjuter</serbian>
</word>
<word>
<english>prison</english>
<serbian>zatvor</serbian>
</word>
</root>
我的解组类是:
字:
package rest;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
/**
* <p>Java class for anonymous complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="english" type="{http://www.w3.org/2001/XMLSchema}string"/>
* <element name="serbian" type="{http://www.w3.org/2001/XMLSchema}string"/>
* </sequence>
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"english",
"serbian"
})
@XmlRootElement(name = "word")
public class Word {
@XmlElement(required = true)
protected String english;
@XmlElement(required = true)
protected String serbian;
/**
* Gets the value of the english property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getEnglish() {
return english;
}
/**
* Sets the value of the english property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setEnglish(String value) {
this.english = value;
}
/**
* Gets the value of the serbian property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getSerbian() {
return serbian;
}
/**
* Sets the value of the serbian property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setSerbian(String value) {
this.serbian = value;
}
}
根:
package rest;
import java.util.ArrayList;
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.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
/**
* <p>Java class for anonymous complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* <complexType>
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element ref="{}word" maxOccurs="unbounded"/>
* </sequence>
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"word"
})
@XmlRootElement(name = "root")
public class Root {
@XmlElement(required = true)
protected List<Word> word;
/**
* Gets the value of the word property.
*
* <p>
* This accessor method returns a reference to the live list,
* not a snapshot. Therefore any modification you make to the
* returned list will be present inside the JAXB object.
* This is why there is not a <CODE>set</CODE> method for the word property.
*
* <p>
* For example, to add a new item, do as follows:
* <pre>
* getWord().add(newItem);
* </pre>
*
*
* <p>
* Objects of the following type(s) are allowed in the list
* {@link Word }
*
*
*/
public List<Word> getWord() {
if (word == null) {
word = new ArrayList<Word>();
}
return this.word;
}
}
和ObjectFactory:
package rest;
import javax.xml.bind.annotation.XmlRegistry;
/**
* This object contains factory methods for each
* Java content interface and Java element interface
* generated in the rest package.
* <p>An ObjectFactory allows you to programatically
* construct new instances of the Java representation
* for XML content. The Java representation of XML
* content can consist of schema derived interfaces
* and classes representing the binding of schema
* type definitions, element declarations and model
* groups. Factory methods for each of these are
* provided in this class.
*
*/
@XmlRegistry
public class ObjectFactory {
/**
* Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: rest
*
*/
public ObjectFactory() {
}
/**
* Create an instance of {@link Root }
*
*/
public Root createRoot() {
return new Root();
}
/**
* Create an instance of {@link Word }
*
*/
public Word createWord() {
return new Word();
}
}
因此,除了一件事之外,一切都很棒。 在我的课堂(第一个代码)中,无法识别列表“单词”。 这里有提示“创建局部变量“单词””。 因此,当我想比较列表中的未编组值(在这种情况下为单词)以及通过Web服务输入的内容时,该怎么办?
在您的TranslatorImp中,“单词”是未定义的符号,因为它是在Main类的main方法中定义的。 该“字”的可见性受到该方法边界的限制。 您应该将解组代码移动到转换方法(虽然性能不佳,但是会让您的代码捕获XML文件的更改并使用该文件中的最新数据),或者引入一种机制来允许将未编组的对象从一类传达到另一类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.