日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

jaxb xml配置_JAXB和Log4j XML配置文件

發布時間:2023/12/3 asp.net 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 jaxb xml配置_JAXB和Log4j XML配置文件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

jaxb xml配置

Log4j 1.x和Log4j 2.x均支持使用XML文件來指定日志記錄配置 。 這篇文章探討了與使用JAXB通過Java類處理這些XML配置文件相關的一些細微差別。 本文中的示例基于Apache Log4j 1.2.17 , Apache Log4j 2.6.2和Java 1.8.0_73(帶有JAXB xjc 2.2.8-b130911.1802)。

Log4j 1.x的XML語法由DTD而不是W3C XML Schema定義 。 幸運的是,JDK附帶的JAXB實現提供了一個“實驗性的,不受支持的”選項,可以將DTD用作生成Java類的輸入。 以下命令可用于對log4j.dtd運行xjc命令行工具 。

xjc -p dustin.examples.l4j1 -d src -dtd log4j.dtd

下一個屏幕快照對此進行了演示。

運行上述命令并在屏幕快照中演示了該命令,這導致在src目錄中的Java程序包(稱為dustin.examples.l4fj1中生成Java類, dustin.examples.l4fj1允許從兼容log4j.dtd的XML解log4j.dtd并編入log4j.dtd兼容的XML。

Log4j 2.x的XML配置可以是“簡潔”或“嚴格”的,在本文中我需要使用“ strict ”,因為這是使用W3C XML Schema文件Log4j-config.xsd和I定義的語法的形式。 需要一個模式來使用JAXB生成Java類。 可以針對此XML模式運行以下命令,以生成表示Log4j2嚴格XML的Java類。

xjc -p dustin.examples.l4j2 -d src Log4j-config.xsd -b l4j2.jxb

運行上面的命令導致Java類的Java包中所產生src目錄下名為dustin.examples.l4j2允許從解組Log4j-config.xsd兼容XML和編組到Log4j-config.xsd - 兼容的XML 。

在上一個示例中,我包括一個帶有選項-b的JAXB綁定文件 ,后跟該綁定文件的名稱( -b l4j2.jxb )。 需要使用此綁定來避免錯誤,該錯誤阻止xjc生成具有錯誤消息“屬性“值”的Log4j 2.x兼容Java類。 使用<jaxb:property>解決此沖突。” 在百慕大的 “ 屬性”值“屬性”中的“ 英國人 ”中已經討論了此問題及其解決方法。 用于解決此沖突 。 接下來顯示我在這里使用的JAXB綁定文件的源。

l4j2.jxb

<jxb:bindings version="2.0"xmlns:jxb="http://java.sun.com/xml/ns/jaxb"xmlns:xsd="http://www.w3.org/2001/XMLSchema"><jxb:bindings schemaLocation="Log4j-config.xsd" node="/xsd:schema"><jxb:bindings node="//xsd:complexType[@name='KeyValuePairType']"><jxb:bindings node=".//xsd:attribute[@name='value']"><jxb:property name="pairValue"/></jxb:bindings></jxb:bindings></jxb:bindings> </jxb:bindings>

剛剛顯示的JAXB綁定文件允許xjc成功解析XSD并生成Java類。 要付出的一小筆代價(除了編寫和引用綁定文件之外)是,將需要在Java類中將KeyValuePairType的“ value”屬性作為名為pairValue而不是value的字段進行訪問。

解組

使用Log4j 1.x的log4j.dtd和Log4j 2.x的Log-config.xsd JAXB生成的類的潛在用例是將 Log4j 1.x XML配置文件轉換為Log4j 2.x “嚴格” XML配置文件 。 在這種情況下,需要解log4j.dtd兼容Log4j 1.x log4j.dtd的XML和編組log4j.dtd Log4j 2.x Log4j-config.xsd的XML。

下面的代碼清單演示了如何使用先前生成的JAXB類解組Log4j 1.x XML。

/*** Extract the contents of the Log4j 1.x XML configuration file* with the provided path/name.** @param log4j1XmlFileName Path/name of Log4j 1.x XML config file.* @return Contents of Log4j 1.x configuration file.* @throws RuntimeException Thrown if exception occurs that prevents* extracting contents from XML with provided name.*/public Log4JConfiguration readLog4j1Config(final String log4j1XmlFileName)throws RuntimeException{Log4JConfiguration config;try{final File inputFile = new File(log4j1XmlFileName);if (!inputFile.isFile()){throw new RuntimeException(log4j1XmlFileName + " is NOT a parseable file.");}final SAXParserFactory spf = SAXParserFactory.newInstance();final SAXParser sp = spf.newSAXParser();final XMLReader xr = sp.getXMLReader();final JAXBContext jaxbContext = JAXBContext.newInstance("dustin.examples.l4j1");final Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();final UnmarshallerHandler unmarshallerHandler = unmarshaller.getUnmarshallerHandler();xr.setContentHandler(unmarshallerHandler);final FileInputStream xmlStream = new FileInputStream(log4j1XmlFileName);final InputSource xmlSource = new InputSource(xmlStream);xr.parse(xmlSource);final Object unmarshalledObject = unmarshallerHandler.getResult();config = (Log4JConfiguration) unmarshalledObject;}catch (JAXBException | ParserConfigurationException | SAXException | IOException exception){throw new RuntimeException("Unable to read from file " + log4j1XmlFileName + " - " + exception,exception);}return config;}

由于log4j.dtd的名稱空間處理的性質,因此將Log4j 1.x XML解組比某些XML解組要復雜一些。 Gik的Jaxb UnMarshall(不帶名稱空間)和Deepa S的 “ 如何指示JAXB忽略命名空間 ”中描述了這種處理皺紋的方法。 使用此方法有助于避免錯誤消息:


UnmarshalException:意外元素(uri:“ http://jakarta.apache.org/log4j/”,local:“ configuration”)。 預期要素...

為了解組我在本例中引用log4j.dtd的Log4j 1.x,在使用Java 8運行此代碼時,我需要為Java啟動器提供特殊的Java系統屬性。
-Djavax.xml.accessExternalDTD=all
為避免出現錯誤消息,“由于accessExternalDTD屬性設置的限制,由于不允許'文件'訪問,因此無法讀取外部DTD。” 可以在NetBeans的FaqWSDLExternalSchema Wiki頁面上找到有關此內容的其他詳細信息。

如下面的示例代碼所示,使用JAXB生成的Java類編組Log4j 2.x XML非常簡單。

/*** Write Log4j 2.x "strict" XML configuration to file with* provided name based on provided content.** @param log4j2Configuration Content to be written to Log4j 2.x* XML configuration file.* @param log4j2XmlFile File to which Log4j 2.x "strict" XML* configuration should be written.*/public void writeStrictLog4j2Config(final ConfigurationType log4j2Configuration,final String log4j2XmlFile){try (final OutputStream os = new FileOutputStream(log4j2XmlFile)){final JAXBContext jc = JAXBContext.newInstance("dustin.examples.l4j2");final Marshaller marshaller = jc.createMarshaller();marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);marshaller.marshal(new ObjectFactory().createConfiguration(log4j2Configuration), os);}catch (JAXBException | IOException exception){throw new RuntimeException("Unable to write Log4 2.x XML configuration - " + exception,exception);}}

在這種編組情況下,有一個微妙之處在剛剛顯示的代碼清單中可能并不明顯。 從Log4j-config.xsd生成的JAXB xjc的類缺少任何帶有@XmlRootElement的類。 從Log4j 1.x log4j.dtd生成的JAXB類確實包含帶有此@XmlRootElement批注的類。 由于基于Log4j 2.x Log4j-config.xsd的Java類沒有此注釋,因此在嘗試直接封送ConfigurationType實例時會發生以下錯誤:


MarshalException –帶有鏈接的異常:[com.sun.istack.internal.SAXException2:由于缺少@XmlRootElement批注,因此無法將類型為“ dustin.examples.l4j2.ConfigurationType”的元素封送為元素]

為避免此錯誤,我改編了(上面代碼清單的第18行new ObjectFactory().createConfiguration(ConfigurationType) ,對傳入的ConfigurationType實例上調用new ObjectFactory().createConfiguration(ConfigurationType)進行了編組,現在已將其成功編組。

結論

JAXB可用于從Log4j 1.x的log4j.dtd和Log4j 2.x的Log4j-config.xsd生成Java類,但是與此過程相關的一些細微差別可以成功地生成這些Java類并使用生成的Java封送和封送XML的類。

翻譯自: https://www.javacodegeeks.com/2016/07/jaxb-log4j-xml-configuration-files.html

jaxb xml配置

總結

以上是生活随笔為你收集整理的jaxb xml配置_JAXB和Log4j XML配置文件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。