Java XML getAttribute

时间:2011-06-14 08:54:13

标签: java xml

我正在尝试从XML文档中获取属性ID(fileID)以用作XML拆分的文件名。拆分工作我只需要提取fileID作为名称。

我可以将此作为帮助。

这是我的xml文档

<root>
 <envelope fileID="000152OP.XML">
   <record id="850">
   </record>
</envelope>
<envelope fileID="000153OP.XML">
  <record id="850">
  </record>
</envelope>
<envelope fileID="000154OP.XML">
  <record id="850">
  </record>
</envelope>
</root>

这是我的Java代码[EDITED]我现在可以读取该属性,但它不会创建最后一个xml文件。所以在我的例子中,它创建了前2个文件,名称正确,但最后没有创建文件ID“000154OP.XML”。

    public static void splitXMLFile (String file) throws Exception {         
    String[] temp;
    String[] temp2;
    String[] temp3;
    String[] temp4;
    String[] temp5;
    String[] temp6;
    File input = new File(file);         
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();         
    Document doc = dbf.newDocumentBuilder().parse(input);
    XPath xpath = XPathFactory.newInstance().newXPath();          
    NodeList nodes = (NodeList) xpath.evaluate("//root/envelope", doc, XPathConstants.NODESET);          
    int itemsPerFile = 1;         

    Node staff = doc.getElementsByTagName("envelope").item(0);

    NamedNodeMap attr = staff.getAttributes();
    Node nodeAttr = attr.getNamedItem("fileID");
    String node = nodeAttr.toString();
    temp = node.split("=");
    temp2 = temp[1].split("^\"");
    temp3 = temp2[1].split("\\.");

    Document currentDoc = dbf.newDocumentBuilder().newDocument();         
    Node rootNode = currentDoc.createElement("root");   
    File currentFile = new File("C:\\XMLFiles\\" + temp3[0]+ ".xml"); 

    for (int i=1; i <= nodes.getLength(); i++) {             
        Node imported = currentDoc.importNode(nodes.item(i-1), true);             
        rootNode.appendChild(imported); 

        Node staff2 = doc.getElementsByTagName("envelope").item(i);
        NamedNodeMap attr2 = staff2.getAttributes();
        Node nodeAttr2 = attr2.getNamedItem("fileID");
        String node2 = nodeAttr2.toString();
        temp4 = node2.split("=");
        temp5 = temp4[1].split("^\"");
        temp6 = temp5[1].split("\\.");

        if (i % itemsPerFile == 0) { 

            writeToFile(rootNode, currentFile);                  
            rootNode = currentDoc.createElement("root");    
            currentFile = new File("C:\\XMLFiles\\" + temp6[0]+".xml");


        }         
    }          
    writeToFile(rootNode, currentFile);     
}    

 private static void writeToFile(Node node, File file) throws Exception {         
     Transformer transformer = TransformerFactory.newInstance().newTransformer();         
     transformer.transform(new DOMSource(node), new StreamResult(new FileWriter(file)));     
 } 

2 个答案:

答案 0 :(得分:2)

也许您可以尝试以下xpath:

//root/envelope/record/@id

如果您使用的XPath库不支持整个XPath集,您可以尝试使用优秀的库jaxen

答案 1 :(得分:1)

ArrayList<String> files = new ArrayList<String>();   
SAXBuilder builder = new SAXBuilder();
Document Doc;
    try {
        Doc = builder.build(new File(myxmlfile.xml));
        Element root = Doc.getRootElement();
        List<Element> category = root.getChildren();
        for(int i=0 ; i < category.size(); i++) {
            Element elem = category.get(i);
            String file = elem.getAttributeValue("fileID");
                            files.add(file);
        }

    } catch (Exception e) {

    }

这将为您提供XML文件中fileIds的数组列表。我使用SAX阅读器来解析XML。