简体   繁体   English

使用jaxb或dom进行Java XML解析?

[英]java XML parsing using jaxb or dom?

I have a following xml file: 我有以下xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<Abc version="3" xmlns="urn:Abc-v3">
  <Ele1>      
    <SubElement name ="name" description="DO">
      <Node version="C" siteURL="https://example.com" />
      <Client>
        <ClientId>1</ClientId>
        <ClientSecret>Yes</ClientSecret>
      </Client>   
    </SubElement>    
    <SubElement name ="SharePointOLDev1" description="DEV1">
      <Node version="C" siteURL="https://example.com" />    
      <Local>
        <LocalId>id</LocalId>
        <Password>password</Password>
      </Local>
    </SubElement>
    <SubElement name="AS" description="wow">
      <DB connection="connection" />
    </SubElement>
  </Ele1>
  <Ele2>
    <Content ID="A" co="LD">
      <Description>Simple Docs</Description>
      <Serve    
        Lib1="TEST"
        Lib2="yes"
        Lib3="yes"
        Lib4="no"
        Lib5="no"
        Lib6="name">
        <Hole enabled="false" count="200"  />

        <Fol enabled="false" count="100"  />

        <Role enabled="No" validate="false" />
        <FetchFilenameAttribute connection="SAP-AS" delay="3" />
      </Serve>
     </Content>
  <Content ID="B" co="OL">
      <Description>Simple Docs </Description>
      <Serve    
        Lib1="TEST"
        Lib2="yes"
        Lib3="yes"
        Lib4="no"
        Lib5="no"
        Lib6="name"">
        <Hole enabled="false" count="200"  />

        <Fol enabled="false" count="100"  />

        <Role enabled="No" validate="false" />
      </Serve>
     </Content>
  </Ele2>
  <Ele3>
    <CNode attr="hai" attr1="bye" />
  </Ele3>

</Abc>

I need to parse this XML file and assign values to its corresponding class objects.Which is the best option to parse such an xml file. 我需要解析此XML文件并将值分配给其对应的类对象。哪个是解析此类xml文件的最佳选择。 JAXB sounds good to me but the POJOs were already written by someone and now i will have to rewrite and deploy them.ALso teh xml file has some errors while running xjc command. JAXB对我来说听起来不错,但是POJO已经有人写过了,现在我将不得不重写和部署它们。而且,运行xjc命令时xml文件有一些错误。 DOM approach seems to be very cumbersome n error prone. DOM方法似乎非常麻烦且容易出错。 Please suggest. 请提出建议。 PS:Kindly excuse my beginner level knowledge. PS:请原谅我的初学者水平。

the JDK project comes with SAX(Simple API for XML) accessible by importing org.xml.sax.* . JDK项目带有SAX(XML的简单API),可以通过导入org.xml.sax.*访问。 You may take a look at this https://www.tutorialspoint.com/java_xml/java_sax_parse_document.htm for an introduction to the subject. 您可以查看此https://www.tutorialspoint.com/java_xml/java_sax_parse_document.htm以获取有关该主题的介绍。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM