【问题标题】:Reading xml document in java application在java应用程序中读取xml文档
【发布时间】:2018-12-04 22:27:46
【问题描述】:

我已经做了很多解决方案,但没有一个能奏效。我如何在 java 中打印这个 XML 代码。主要问题是如何打印值 xml:lang。

<resource xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ofbiz.apache.org/dtds/ofbiz-properties.xsd">
<property key="hour.plural">
    <value xml:lang="ar">ساعات</value>
    <value xml:lang="de">Stunden</value>

<value xml:lang="pl">oglądaj</value></property></resource>

这是资源类

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="resource")
public class resource {

    private String property;
    private String value;

    public root(String property, String value) {
        this.property = property;
        this.value = value;
    }

    public String getProperty() {
        return property;
    }

    public String getValue() {
        return value;
    }
    public String toString() {
        return property + value;
    }
}

这是主要的类

import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import project1.resource;
    public class xmlread {

        public static void main(String... arg) throws Exception {
            File file = new File("C:DateTimeLabels.xml");
            JAXBContext jaxbContext = JAXBContext.newInstance(resource.class);

            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            resource Resource = (resource) jaxbUnmarshaller.unmarshal(file);
            System.out.println(Resource);
    }
}

【问题讨论】:

  • 请显示您的尝试,即使它们没有成功,以便我们指出其中是否有任何错误。
  • 要打印它,您可以使用一个简单的字符串并将其写入System.out。我想你的问题更复杂。请edit您的问题,并准确说明您的输入和输出必须是什么,您的困难所在,以及您所做的研究。
  • 您的问题自相矛盾。阅读和打印不是一回事。您实际上在问哪个:阅读或打印?另外,我同意迈克尔的观点。如果您向我们展示您的代码,我们会更好地了解您正在尝试做什么,以及您可能做错了什么。
  • 我已经编辑了我的问题。
  • 我向你保证你没有编辑你的问题

标签: java xml jaxb


【解决方案1】:
@XmlRootElement(name="resource")
public class resource {

    private String property;
    private String value;

    public root(String property, String value) {
        this.property = property;
        this.value = value;
    }
    @XmlElement(name = "property")
    public String getProperty() {
        return property;
    }
    @XmlElement(name = "value")
    public String getValue() {
        return value;
    }
    public String toString() {
        return property + value;
    }
}`

将此添加到您的资源类中,应该可以帮助编组器找到您想要的确切属性。

【讨论】:

    【解决方案2】:

    您有多个值,因此为了&lt;value&gt; 元素,您需要定义一个值列表。

    元素包含在元素内部,所以这里有一个嵌套结构。这意味着您需要在 Resource 类中包含一个 Property 类,并且您的 Property 类需要包含一个 Value 类。

    需要绑定key属性和xml:lang属性。

    这里是一个带有 Jaxb 注释的类的示例,它完成了大部分工作以映射到您的 XML 格式。根据需要完成。

    @XmlRootElement
    public class Resource {
      private Property property = new Property();
    
      public Property getProperty() {
        return property;
      }
    
      public void setProperty(Property property) {
        this.property = property;
      }
    
      public static void main(String[] args) throws JAXBException {
        JAXBContext context = JAXBContext.newInstance(Resource.class);
        Marshaller marshaller = context.createMarshaller();
    
        Resource resource = new Resource();
        resource.getProperty().setKey("hour.plural");
        resource.getProperty().getValues().add(new Value("fr"));
        resource.getProperty().getValues().add(new Value("it"));
        resource.getProperty().getValues().add(new Value("ru"));
    
        marshaller.marshal(resource, System.out);
      }
    
    }
    
    class Property {
      private String key;
    
      private List<Value> values = new ArrayList<>();
    
      @XmlAttribute
      public String getKey() {
        return key;
      }
    
      public void setKey(String key) {
        this.key = key;
      }
    
      @XmlElement(name = "value")
      public List<Value> getValues() {
        return values;
      }
    
      public void setValues(List<Value> values) {
        this.values = values;
      }
    
    }
    
    class Value {
      private String lang;
    
      public Value() {
    
      }
    
      public Value(String lang) {
        this.lang = lang;
      }
    
      @XmlAttribute
      public String getLang() {
        return lang;
      }
    
      public void setLang(String lang) {
        this.lang = lang;
      }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-30
      • 1970-01-01
      • 1970-01-01
      • 2012-08-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多