尝试将XML转换为允许用户输入的JTable

时间:2016-11-15 18:01:29

标签: java xml parsing jtable

我有一个像这样的XML文件



<data>
  <columns>
    <Brand index="5">Brand</Brand>
    <BuyCurrency index="13">BuyCurrency</BuyCurrency>
    <BuyPrice index="12">BuyPrice</BuyPrice>
    <Category index="3">Category</Category>
    <Id index="1">Id</Id>
    <LeadTime index="16">LeadTime</LeadTime>
    <MinOrderQuantity index="7">MinOrderQuantity</MinOrderQuantity>
    <Name index="2">Name</Name>
    <SellCurrency index="11">SellCurrency</SellCurrency>
    <SellPrice index="10">SellPrice</SellPrice>
    <StockOnHand index="14">StockOnHand</StockOnHand>
    <StockOnOrder index="15">StockOnOrder</StockOnOrder>
    <SubCategory index="4">SubCategory</SubCategory>
    <Supplier index="6">Supplier</Supplier>
    <TargetBatchVolume index="8">TargetBatchVolume</TargetBatchVolume>
    <Volume index="9">Volume</Volume>
  </columns>
  <records>
    <record index="1">
      <Brand>StarTAC</Brand>
      <BuyCurrency>GBP</BuyCurrency>
      <BuyPrice>27.67</BuyPrice>
      <Category>Technology</Category>
      <Id>16342939</Id>
      <LeadTime>15</LeadTime>
      <MinOrderQuantity>1</MinOrderQuantity>
      <Name>StarTAC Series</Name>
      <SellCurrency>USD</SellCurrency>
      <SellPrice>65.99</SellPrice>
      <StockOnHand>15</StockOnHand>
      <StockOnOrder>50</StockOnOrder>
      <SubCategory>Telephones and Communication</SubCategory>
      <Supplier>Office First</Supplier>
      <TargetBatchVolume>0</TargetBatchVolume>
      <Volume>0.8</Volume>
    </record>
    <record index="2">
      <Brand>Xerox</Brand>
      <BuyCurrency>CNY</BuyCurrency>
      <BuyPrice>29.8</BuyPrice>
      <Category>Office Supplies</Category>
      <Id>16346727</Id>
      <LeadTime>5</LeadTime>
      <MinOrderQuantity>1</MinOrderQuantity>
      <Name>Xerox 1984</Name>
      <SellCurrency>USD</SellCurrency>
      <SellPrice>6.48</SellPrice>
      <StockOnHand>34</StockOnHand>
      <StockOnOrder>1</StockOnOrder>
      <SubCategory>Paper</SubCategory>
      <Supplier>Drecom</Supplier>
      <TargetBatchVolume>67.5</TargetBatchVolume>
      <Volume>0.47</Volume>
    </record>
    <record index="3">
      <Brand>Xerox</Brand>
      <BuyCurrency>CNY</BuyCurrency>
      <BuyPrice>219.54</BuyPrice>
      <Category>Office Supplies</Category>
      <Id>16346853</Id>
      <LeadTime>10</LeadTime>
      <MinOrderQuantity>1</MinOrderQuantity>
      <Name>Xerox 1885</Name>
      <SellCurrency>USD</SellCurrency>
      <SellPrice>48.04</SellPrice>
      <StockOnHand>16</StockOnHand>
      <StockOnOrder>40</StockOnOrder>
      <SubCategory>Paper</SubCategory>
      <Supplier>Drecom</Supplier>
      <TargetBatchVolume>67.5</TargetBatchVolume>
      <Volume>0.55</Volume>
    </record>
    <record index="4">
      <Brand>Elite</Brand>
      <BuyCurrency>CNY</BuyCurrency>
      <BuyPrice>34.12</BuyPrice>
      <Category>Office Supplies</Category>
      <Id>16350870</Id>
      <LeadTime>12</LeadTime>
      <MinOrderQuantity>1</MinOrderQuantity>
      <Name>Elite 5 inch Scissors</Name>
      <SellCurrency>USD</SellCurrency>
      <SellPrice>8.45</SellPrice>
      <StockOnHand>0</StockOnHand>
      <StockOnOrder>3</StockOnOrder>
      <SubCategory>Scissors, Rulers and Trimmers</SubCategory>
      <Supplier>FHL</Supplier>
      <TargetBatchVolume>0</TargetBatchVolume>
      <Volume>0.85</Volume>
    </record>
    <record index="5">
      <Brand>Wilson</Brand>
      <BuyCurrency>CNY</BuyCurrency>
      <BuyPrice>31.3</BuyPrice>
      <Category>Office Supplies</Category>
      <Id>16359070</Id>
      <LeadTime>33</LeadTime>
      <MinOrderQuantity>10</MinOrderQuantity>
      <Name>Wilson Jones DublLock D-Ring Binders</Name>
      <SellCurrency>USD</SellCurrency>
      <SellPrice>6.75</SellPrice>
      <StockOnHand>30</StockOnHand>
      <StockOnOrder>0</StockOnOrder>
      <SubCategory>Binders and Binder Accessories</SubCategory>
      <Supplier>SuperSupply</Supplier>
      <TargetBatchVolume>0</TargetBatchVolume>
      <Volume>0.12</Volume>
    </record>
    <record index="6">
      <Brand>MicroTAC</Brand>
      <BuyCurrency>CNY</BuyCurrency>
      <BuyPrice>261.48</BuyPrice>
      <Category>Technology</Category>
      <Id>16376266</Id>
      <LeadTime>5</LeadTime>
      <MinOrderQuantity>1</MinOrderQuantity>
      <Name>MicroTAC 650</Name>
      <SellCurrency>USD</SellCurrency>
      <SellPrice>65.99</SellPrice>
      <StockOnHand>5</StockOnHand>
      <StockOnOrder>1</StockOnOrder>
      <SubCategory>Telephones and Communication</SubCategory>
      <Supplier>Drecom</Supplier>
      <TargetBatchVolume>67.5</TargetBatchVolume>
      <Volume>0.55</Volume>
    </record>
&#13;
&#13;
&#13;

然后我用DOM Parser解析它,它变成了:

&#13;
&#13;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.swing.JTable;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;

import java.util.*;
import java.io.File;
import java.util.ArrayList;

public class inventoryread {

  public static void main(String argv[]) {

    try {

	File fXmlFile = new File("sample.xml");
	DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
	DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
	Document doc = dBuilder.parse(fXmlFile);

	doc.getDocumentElement().normalize();

	System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

	NodeList nList = doc.getElementsByTagName("record");

	System.out.println("----------------------------");

	for (int temp = 0; temp < nList.getLength(); temp++) {
		List<String> record = new ArrayList<>();

		Node nNode = nList.item(temp);

		System.out.println("\nCurrent Element :" + nNode.getNodeName());

		if (nNode.getNodeType() == Node.ELEMENT_NODE) {

			Element eElement = (Element) nNode;

			System.out.println("record index " + eElement.getAttribute("index"));
			System.out.println("Buy Currency : " + eElement.getElementsByTagName("BuyCurrency").item(0).getTextContent());
			System.out.println("Buy Price : " + eElement.getElementsByTagName("BuyPrice").item(0).getTextContent());
			System.out.println("Category : " + eElement.getElementsByTagName("Category").item(0).getTextContent());
			System.out.println("Id : " + eElement.getElementsByTagName("Id").item(0).getTextContent());
			System.out.println("LeadTime : " + eElement.getElementsByTagName("LeadTime").item(0).getTextContent());
			System.out.println("MinOrderQuantity : " + eElement.getElementsByTagName("MinOrderQuantity").item(0).getTextContent());
			System.out.println("Name : " + eElement.getElementsByTagName("Name").item(0).getTextContent());
			System.out.println("SellCurrency : " + eElement.getElementsByTagName("SellCurrency").item(0).getTextContent());
			System.out.println("SellPrice : " + eElement.getElementsByTagName("SellPrice").item(0).getTextContent());
			System.out.println("StockOnHand : " + eElement.getElementsByTagName("StockOnHand").item(0).getTextContent());
			System.out.println("StockOnOrder : " + eElement.getElementsByTagName("StockOnOrder").item(0).getTextContent());
			System.out.println("SubCategory : " + eElement.getElementsByTagName("SubCategory").item(0).getTextContent());
			System.out.println("Supplier : " + eElement.getElementsByTagName("Supplier").item(0).getTextContent());
			System.out.println("TargetBatchVolume : " + eElement.getElementsByTagName("TargetBatchVolume").item(0).getTextContent());
			System.out.println("Volume : " + eElement.getElementsByTagName("Volume").item(0).getTextContent());
		
		}
		
	}
    } catch (Exception e) {
	e.printStackTrace();
    }
  }
&#13;
&#13;
&#13;

现在我需要将数据转换为JTable,用户能够更改数据,我做了研究,完全陷入困境。有人请给我一些帮助和解决方案吗?

感谢!!!!

1 个答案:

答案 0 :(得分:0)

将您的数据提取到String [] []数据,这将是您需要向用户显示的值,以及String [] columnName的列名,它们将是每列的名称。 第一种方法将创建JFrame,其中将显示您的表,如果您需要保存数据,则可以使用第二种方法来检索数据。

String [][] data = new String[][]{{"Test11", "Test12", "Test13", "Test14"},
            {"Test21", "Test22", "Test23", "Test24"},
            {"Test31", "Test32", "Test33", "Test34"}};
String[] columnName = new String[] {"Column1", "Column2", "Column3", "Column3"};


private void createDataTable(String[][] data, String[] columnName){
    JFrame frame = new JFrame();
    JScrollPane scrollPane = new JScrollPane();

    JTable table = new JTable(data, columnName);
    scrollPane.setViewportView(table);
    frame.add(scrollPane);

    frame.setSize(800, 800);
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    frame.setVisible(true);

}

public String[][] getTableData (JTable table) {
    TableModel dtm = table.getModel();
    int nRow = dtm.getRowCount(), nCol = dtm.getColumnCount();
    String[][] tableData = new String[nRow][nCol];
    for (int i = 0 ; i < nRow ; i++)
        for (int j = 0 ; j < nCol ; j++)
            tableData[i][j] = dtm.getValueAt(i,j).toString();
    return tableData;
}