【问题标题】:Replace removed namespace details on Unmarshal替换 Unmarshal 上已删除的命名空间详细信息
【发布时间】:2014-02-04 12:16:36
【问题描述】:

我有一个业务需求,即从类(在 Schema 中定义)以尽可能轻量级的形式生成 XML。为此,我从 xml 中删除了所有命名空间信息。

示例架构:

<xs:schema xmlns="http://aschema/" 
           xmlns:xs="http://www.w3.org/2001/XMLSchema" 
           targetNamespace="http://aschema/" 
           elementFormDefault="qualified" 
           attributeFormDefault="unqualified">

  <xs:element name="Test" type="TestType"/>
  <xs:complexType name="TestType">
    <xs:sequence>
      <xs:element name="test" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

生成Test 类:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "TestType", propOrder = {
    "test"
})
@XmlRootElement(name = "Test")
public class Test {

    @XmlElement(required = true)
    protected String test;

    /**
     * Gets the value of the test property.
     * 
     * @return
     *     possible object is
     *     {@link String }
     *     
     */
    public String getTest() {
        return test;
    }

    /**
     * Sets the value of the test property.
     * 
     * @param value
     *     allowed object is
     *     {@link String }
     *     
     */
    public void setTest(String value) {
        this.test = value;
    }

}

Marshals to(如果您愿意,我可以解释一下,但我认为这只会使问题变得混乱 - 如果需要,很乐意添加):

<Test><test>Hello World!</test></Test>

现在我想解组它。我明白了(并不奇怪):

javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"Test"). Expected elements are <{http://aschema/}Test>

有什么方法可以定义我的解组过程遇到这个问题时使用什么?

我无法更改架构 - 这是行业标准。

我无法更改 Test 对象 - 它是从架构生成的。

我能找到的对这个错误的所有引用似乎要么指向 java 类的变化,要么指向架构的变化——我不能做这两件事。

请注意,我的 Test 类是在尝试创建 Minimal, Complete, Tested and Readable example。我要解组的真实对象要复杂得多。

【问题讨论】:

标签: java xml jaxb unmarshalling


【解决方案1】:

像往常一样 - 一旦我发布了问题,就会出现解决方案 - 就像魔术一样。这就是你的 StackOverflow。

来自JAXB unmarshal with declared type does not populate the resulting object with data

public class NamespaceFilter extends XMLFilterImpl {

  private static final String NAMESPACE = "http://aschema/";

  @Override
  public void endElement(String uri, String localName, String qName) throws SAXException {
    super.endElement(NAMESPACE, localName, qName);
  }

  @Override
  public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
    super.startElement(NAMESPACE, localName, qName, atts);
  }

}

...

  // Create the XMLFilter
  XMLFilter filter = new NamespaceFilter();

  // Set the parent XMLReader on the XMLFilter
  SAXParserFactory spf = SAXParserFactory.newInstance();
  SAXParser sp = spf.newSAXParser();
  XMLReader xr = sp.getXMLReader();
  filter.setParent(xr);

  // Set UnmarshallerHandler as ContentHandler on XMLFilter
  Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
  UnmarshallerHandler unmarshallerHandler = unmarshaller.getUnmarshallerHandler();
  filter.setContentHandler(unmarshallerHandler);

  // Parse the XML
  filter.parse(new InputSource(r));
  //Object result = unmarshallerHandler.getResult();  
  T t = (T) unmarshallerHandler.getResult();

【讨论】:

  • @BlaiseDoughan - 我很抱歉 Blaise - 在我的研究过程中,我遇到了你的很多工作,这一切都非常有帮助。按照建议进行了当之无愧的投票。 :)
猜你喜欢
  • 2019-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多