解析xml中的一些元素

时间:2013-09-03 02:17:54

标签: java xml xml-parsing

我想知道是否可以从xml文件解析一些属性,成为java中的对象

我不想创建xml中的所有字段。

那么,我该怎么做呢?

例如下面有一个xml文件,我只想要标签内的数据。

<emit>
<CNPJ>1109</CNPJ>
<xNome>OESTE</xNome>
<xFant>ABATEDOURO</xFant>
<enderEmit>
    <xLgr>RODOVIA</xLgr>
    <nro>S/N</nro>
    <xCpl>402</xCpl>
    <xBairro>GOMES</xBairro>
    <cMun>314</cMun>
    <xMun>MINAS</xMun>
    <UF>MG</UF>
    <CEP>35661470</CEP>
    <cPais>58</cPais>
    <xPais>Brasil</xPais>
    <fone>03</fone>
</enderEmit>
<IE>20659</IE>
<CRT>3</CRT>

3 个答案:

答案 0 :(得分:2)

对于没有XSD并且不想创建完整的对象图来表示XML的Java XML解析,JDOM是一个很棒的工具。它允许您轻松地遍历XML树并选择您感兴趣的元素。

以下是一些使用JDOM从XML doc中选择任意值的示例代码:

  // reading can be done using any of the two 'DOM' or 'SAX' parser
  // we have used saxBuilder object here
  // please note that this saxBuilder is not internal sax from jdk
  SAXBuilder saxBuilder = new SAXBuilder();

  // obtain file object
  File file = new File("/tmp/emit.xml");

  try {
     // converted file to document object
     Document document = saxBuilder.build(file);

     //You don't need this or the ns parameters in getChild()
     //if your XML document has no namespace
     Namespace ns = Namespace.getNamespace("http://www.example.com/namespace");

     // get root node from xml.  emit in your sample doc?
     Element rootNode = document.getRootElement();

     //getChild() assumes one and only one, enderEmit element.  Use a lib and error
     //checking as needed for your document
     Element enderEmitElement = rootNode.getChild("enderEmit", ns);

     //now we get two of the child  from
     Element xCplElement = enderEmitElement.getChild("xCpl", ns);
     //should be 402 in your example
     String xCplValue = xCplElement.getText();
     System.out.println("xCpl: " + xCplValue);

     Element cMunElement = enderEmitElement.getChild("cMun", ns);
     //should be 314 in your example
     String cMunValue = cMunElement.getText();
     System.out.println("cMun: " + cMunValue);


  } catch (JDOMException e) {
     e.printStackTrace();
  } catch (IOException e) {
     e.printStackTrace();
  }

答案 1 :(得分:1)

您可以使用JAXB将xml解组为Java对象,使用该对象可以轻松读取选择元素。使用JAXB,给定的XML可以用Java表示,如下所示:

enderEmit元素:

@XmlRootElement
public class EnderEmit{
private String xLgr;
//Other elements.Here you can define properties for only those elements that you want to load
}

emit元素(这代表你的XML文件):

@XmlRootElement
public class Emit{
private String cnpj;
private String xnom;
private EnderEmit enderEmit;
..
//Add elements that you want to load
}

现在通过使用下面的代码行,您可以将xml读取到对象:

String filePath="filePath";
File file = new File(filePath);
JAXBContext jaxbContext = JAXBContext.newInstance(Emit.class);
jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Emit emit = (Emit) jaxbUnmarshaller.unmarshal(file);

该行将为您提供给定xml的emit对象。

答案 2 :(得分:0)

尝试使用StringUtils.subStringBetween

try

    {
        String input = "";
        br = new BufferedReader(new FileReader(FILEPATH)); 
        String result = null;
        while ((input = br.readLine()) != null) // here we read the file line by line
        {
            result = StringUtils.substringBetween(input, ">", "<"); // using StringUtils.subStringBetween to get the data what you want
            if(result != null) // if the result should not be null because some of the line not having the tags
            {
                System.out.println(""+result);
            }
        }
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
    finally
    {
        try
        {
            if (br != null)
            {
                br.close();
            }
        }
        catch (IOException ex)
        {
            ex.printStackTrace();
        }
    }