2016-11-15 3 views
1

Я получил файл XML, который, как этот Попытка превратить XML в JTable, что позволяет пользовательский ввод

<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>

А потом я проанализирован его с DOM Parser, и она становится:

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 { 
 

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

 
\t doc.getDocumentElement().normalize(); 
 

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

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

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

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

 
\t \t Node nNode = nList.item(temp); 
 

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

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

 
\t \t \t Element eElement = (Element) nNode; 
 

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

Теперь мне нужно, чтобы превратить данные в JTable с пользователем, способным сп ange данные, и я делал исследования и просто полностью застрял. Кто-нибудь, пожалуйста, дайте мне помощь и решения?

Спасибо !!!!

ответ

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; 
} 
+0

Я устал бежать, но он, похоже, не работает. Возможно, я допустил ошибку, когда я поместил код в –

+0

. Код работает для данных и columnName, которые я опубликовал. Возможно ли, что вы не правильно извлекли данные в массивы – adi9090

 Смежные вопросы

  • Нет связанных вопросов^_^