0

我想知道我是否可以从 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>

4

3 回答 3

2

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

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

  // 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();
  }
于 2013-09-03T03:19:44.390 回答
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
}

发出元素(这代表您的 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);

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

于 2013-09-03T03:53:34.917 回答
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();
        }
    }
于 2013-09-03T02:23:35.327 回答