创建具有自定义命名空间的Java XML字符串

huangapple go评论134阅读模式
英文:

Create XML string in java with custom namespace

问题

我想在Java中以编程方式生成XML字符串,其命名空间如下所示,并且所有数据必须以动态方式出现在XML中。我如何实现类似这样的内容?

<faxml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:noNamespaceSchemaLocation="test.xsd">
  <person>
    <name>ABC</name>
  </person>
</faxml>

我已经查看了诸如此类的示例:https://howtodoinjava.com/jaxb/write-object-to-xml/,但在这里生成XML时,其起始行是:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

但我想要像我在示例代码中提到的那样,以<faxml>开头和结尾,并带有命名空间。

英文:

I want to generate xml string in java programmatically whose name space is custom as shown below
and all data must come dynamically in xml.How can I achieve something like this?

&lt;faxml xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
xsi:noNamespaceSchemaLocation=&quot;test.xsd&quot;&gt;
&lt;person&gt;
  &lt;name&gt;ABC&lt;/name&gt;
&lt;/person&gt;
&lt;/faxml&gt;

I have gone through examples like this https://howtodoinjava.com/jaxb/write-object-to-xml/ but here when xml generated its starting line is

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;yes&quot;?&gt;

but I want start and end tag &lt;faxml&gt; and namespaces as I mentioned in my sample code as output

答案1

得分: 1

String name = "name";
String createXml = "<faxml xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" +
                  " xsi:noNamespaceSchemaLocation=\"test.xsd\">" +
                  "<person>" +
                  "<name>" + name + "</name>" +
                  "</person>" +
                  "</faxml>";
Sysout(createXml);

get the name in a variable. Hard code these lines and insert it like this..
英文:
	String name= &quot;name&quot;		
	String createXml=&quot;&lt;faxml xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;//
                      xsi:noNamespaceSchemaLocation=&quot;test.xsd&quot;&gt;&quot;//
			          +&quot;&lt;person&gt;&quot;//
			          +&quot;&lt;name&gt;&quot;+name+&quot;&lt;/name&gt;&quot;//
			          +&quot;&lt;/&gt;person&quot;&gt;&quot;//
			          +&quot;&lt;/faxml&gt;&quot;;
                    Sysout(createXml);

get the name in a variable. Hard code these lines and insert it like this..

答案2

得分: 1

这里是一种方法:

首先,这是我表示所需XML数据的类:

package org.ajames.jaxb.persons;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "")
@XmlRootElement(name = "faxml")
public class Faxml {
    
    private Person person;
    
    public static class Person {
        
        private String name;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
        
    }

    public Person getPerson() {
        return person;
    }

    public void setPerson(Person person) {
        this.person = person;
    }
    
}

我选择将Person类嵌套在根类中 - 仅用于此测试。当然,有其他方法来组织这些类,使它们不嵌套。

然后我为org.ajames.jaxb.persons定义了以下的package-info

@XmlSchema(
        namespace = "",
        elementFormDefault = XmlNsForm.UNSET,
        xmlns = {
            @XmlNs(prefix = "", namespaceURI = "http://www.w3.org/2001/XMLSchema-instance")
        })
package org.ajames.jaxb.persons;

import javax.xml.bind.annotation.XmlSchema;
import javax.xml.bind.annotation.XmlNs;
import javax.xml.bind.annotation.XmlNsForm;

要将Java对象的数据处理为XML,我使用以下测试数据:

Faxml.Person person = new Faxml.Person();
person.setName("ABC");
Faxml faxml = new Faxml();
faxml.setPerson(person);

JAXB上下文和编组器如下,假设我们将XML写入Java字符串,用于此测试:

JAXBContext jaxbContext = JAXBContext.newInstance(Faxml.class); 
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
marshaller.setProperty(Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION, "test.xsd");
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        
StringWriter stringWriter = new StringWriter();
marshaller.marshal(faxml, stringWriter);
String xml = stringWriter.toString();
        
System.out.println(xml);

生成的XML如下:

<faxml xsi:noNamespaceSchemaLocation="test.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <person>
        <name>ABC</name>
    </person>
</faxml>
英文:

Here is one approach:

First, here is my class representing the required XML data:

package org.ajames.jaxb.persons;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = &quot;&quot;)
@XmlRootElement(name = &quot;faxml&quot;)
public class Faxml {
    
    private Person person;
    
    public static class Person {
        
        private String name;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
        
    }

    public Person getPerson() {
        return person;
    }

    public void setPerson(Person person) {
        this.person = person;
    }
    
}

I chose to nest the Person class inside the root class - just for this test. There are other ways to arrange these classes, of course, so they are not nested.

Then I define the package-info for org.ajames.jaxb.persons as follows:

@XmlSchema(
        namespace = &quot;&quot;,
        elementFormDefault = XmlNsForm.UNSET,
        xmlns = {
            @XmlNs(prefix = &quot;&quot;, namespaceURI = &quot;http://www.w3.org/2001/XMLSchema-instance&quot;)
        })
package org.ajames.jaxb.persons;

import javax.xml.bind.annotation.XmlSchema;
import javax.xml.bind.annotation.XmlNs;
import javax.xml.bind.annotation.XmlNsForm;

To process the data from a Java object to XML, I use the following test data:

Faxml.Person person = new Faxml.Person();
person.setName(&quot;ABC&quot;);
Faxml faxml = new Faxml();
faxml.setPerson(person);

The JAXB context and marshaller are as follows, assuming we are writing the XML to a Java String, for this test:

JAXBContext jaxbContext = JAXBContext.newInstance(Faxml.class); 
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
marshaller.setProperty(Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION, &quot;test.xsd&quot;);
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        
StringWriter stringWriter = new StringWriter();
marshaller.marshal(faxml, stringWriter);
String xml = stringWriter.toString();
        
System.out.println(xml);

The resulting XML is:

&lt;faxml xsi:noNamespaceSchemaLocation=&quot;test.xsd&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;&gt;
    &lt;person&gt;
        &lt;name&gt;ABC&lt;/name&gt;
    &lt;/person&gt;
&lt;/faxml&gt;

huangapple
  • 本文由 发表于 2020年8月27日 01:59:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/63603271.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定