第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

JAXB - 轉(zhuǎn)換其中包含 XSD/命名空間引用的 XML 文件

JAXB - 轉(zhuǎn)換其中包含 XSD/命名空間引用的 XML 文件

飲歌長(zhǎng)嘯 2023-03-31 14:46:45
如何將帶有 XSD/命名空間的 XML 轉(zhuǎn)換為對(duì)象?我收到此錯(cuò)誤:javax.xml.bind.UnmarshalException:意外元素(uri:“ http://www.opengis.net/wfs/2.0 ”,本地:“FeatureCollection”)。預(yù)期的元素是 <{}FeatureCollection>這是示例 XML:<wfs:FeatureCollection xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fes="http://www.opengis.net/fes/2.0" xmlns:wfs="http://www.opengis.net/wfs/2.0" xmlns:gml="http://www.opengis.net/gml/3.2" xmlns:ows="http://www.opengis.net/ows/1.1" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" numberMatched="7961422" numberReturned="0" timeStamp="2019-07-16T09:44:51.540Z" xsi:schemaLocation="http://www.opengis.net/wfs/2.0 http://schemas.opengis.net/wfs/2.0/wfs.xsd"/>我的簡(jiǎn)單 JAXB 轉(zhuǎn)換器是:public static Object convertXmlToObject(String xmlString, Class targetClass) {    JAXBContext jaxbContext;    try {        jaxbContext = JAXBContext.newInstance( targetClass);        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();        return jaxbUnmarshaller.unmarshal(new StringReader(xmlString));    } catch (JAXBException e) {        e.printStackTrace();        return null;    }}這個(gè)方法的調(diào)用是:TargetObject to = (TargetObject) converter.convertXmlToObject( xmlString, TargetObject.class);目標(biāo)對(duì)象是:@XmlRootElement( name="FeatureCollection")public class TargetObject {    private long numberMatched = -1;    private long numberReturned = -1;    private LocalDateTime timeStamp;    // ... all getters    @XmlAttribute    public void setNumberMatched(long numberMatched) {        this.numberMatched = numberMatched;    }    @XmlAttribute    public void setNumberReturned(long numberReturned) {        this.numberReturned = numberReturned;    }    @XmlAttribute    @XmlJavaTypeAdapter(value = LocalDateTimeAdapter.class)    public void setTimeStamp(LocalDateTime timeStamp) {        this.timeStamp = timeStamp;    }}如何改進(jìn)我的代碼以將 XML 字符串轉(zhuǎn)換為對(duì)象?
查看完整描述

1 回答

?
揚(yáng)帆大魚

TA貢獻(xiàn)1799條經(jīng)驗(yàn) 獲得超9個(gè)贊

享受這個(gè)通用的解決方案:

  • 轉(zhuǎn)換器類

  • Junit 測(cè)試——顯示了 1 個(gè)沒有名稱空間的測(cè)試,但它確實(shí)適用于名稱空間

使用 XSLT 剝離名稱空間的轉(zhuǎn)換器類。JAXB 有時(shí)過于直白,您可以在許多帖子中看到這一點(diǎn)。

public class XmlToPojo {

    public static Object convertXmlToObject(String xmlString, Class targetClass) {

        JAXBContext jaxbContext;

        try {

            // Step 1 - remove namespaces

            StreamSource xmlSource = new StreamSource(new StringReader(xmlString));

            StreamResult result = new StreamResult(new StringWriter());

            removeNamespace(xmlSource, result);

            // Step 2 - convert XML to object

            jaxbContext = JAXBContext.newInstance(targetClass);

            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();

            return jaxbUnmarshaller.unmarshal(new StringReader(result.getWriter().toString()));

        } catch (Exception e) {

            e.printStackTrace();

            return null;

        }

    }


    // Remove namespaces from XML


    private static String xsltNameSpaceRemover = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +

            "<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">\n" +

            "\n" +

            "    <xsl:output indent=\"yes\" method=\"xml\" encoding=\"utf-8\" omit-xml-declaration=\"yes\"/>\n" +

            "\n" +

            "    <!-- Stylesheet to remove all namespaces from a document -->\n" +

            "    <!-- NOTE: this will lead to attribute name clash, if an element contains\n" +

            "        two attributes with same local name but different namespace prefix -->\n" +

            "    <!-- Nodes that cannot have a namespace are copied as such -->\n" +

            "\n" +

            "    <!-- template to copy elements -->\n" +

            "    <xsl:template match=\"*\">\n" +

            "        <xsl:element name=\"{local-name()}\">\n" +

            "            <xsl:apply-templates select=\"@* | node()\"/>\n" +

            "        </xsl:element>\n" +

            "    </xsl:template>\n" +

            "\n" +

            "    <!-- template to copy attributes -->\n" +

            "    <xsl:template match=\"@*\">\n" +

            "        <xsl:attribute name=\"{local-name()}\">\n" +

            "            <xsl:value-of select=\".\"/>\n" +

            "        </xsl:attribute>\n" +

            "    </xsl:template>\n" +

            "\n" +

            "    <!-- template to copy the rest of the nodes -->\n" +

            "    <xsl:template match=\"comment() | text() | processing-instruction()\">\n" +

            "        <xsl:copy/>\n" +

            "    </xsl:template>\n" +

            "\n" +

            "</xsl:stylesheet>";


    private static void removeNamespace(Source xmlSource, Result xmlOutput) throws TransformerException {

        TransformerFactory factory = TransformerFactory.newInstance();

        StreamSource xsltSource = new StreamSource(new StringReader(xsltNameSpaceRemover));

        Transformer transformer = factory.newTransformer(xsltSource);

        transformer.transform(xmlSource, xmlOutput);

    }

}

一個(gè)簡(jiǎn)單的 Junit 測(cè)試:


public class XmlToPojoTest {

    @Test

    public void testBasic() {

        String xmlString = "<employee>" +

                "    <department>" +

                "        <id>101</id>" +

                "        <name>IT-ABC</name>" +

                "    </department>" +

                "    <firstName>JJ</firstName>" +

                "    <id>1</id>" +

                "    <lastName>JoHo</lastName>" +

                "</employee>";

        XmlToPojo xmlToPojo = new XmlToPojo();

        Employee emp = (Employee) xmlToPojo.convertXmlToObject(xmlString, Employee.class);

        assertEquals("JJ", emp.getFirstName());

        assertEquals("IT-ABC", emp.getDepartment().getName());

    }

}


查看完整回答
反對(duì) 回復(fù) 2023-03-31
  • 1 回答
  • 0 關(guān)注
  • 204 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)