我是一个非常初学者,很抱歉提出这些简单的问题。希望得到用户的帮助。谢谢你的时间!
Q1。在控制台上,结果' 5'控制摘要显示在下一行而不是旁边。我可以知道如何获得这样的控制台结果[控制摘要:5]?
Q2。有一个标签' items'在xml.file中它正在包装'项目'及其内容。标签'测试'是根本元素,但这个'项目'标签?任何人都可以告诉我它是什么以及如何在代码中转换为java应用程序?
XML文件
<?xml version="1.0" encoding="utf-8"?>
<test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.stee.com.sg/q2.xsd">
<controlsummary>
<totalrecords>5</totalrecords>
</controlsummary>
<items>
<item>
<data>TEST1</data>
<data_datetime>20120901000000</data_datetime>
<total_a>492.00</total_a>
<total_a_in_unit>TONNES</total_a_in_unit>
<total_b>420.00</total_b>
<total_b_in_unit>TONNES</total_b_in_unit>
<loc_id>1</loc_id>
<loc_desc>Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch</loc_desc>
<company_id>6001</company_id>
<company_name>Great Real Estate pte ltd</company_name>
</item>
<item>
<data>TEST7</data>
<data_datetime>20120901000000</data_datetime>
<total_a>2228.00</total_a>
<total_a_in_unit>TONNES</total_a_in_unit>
<total_b>2260.00</total_b>
<total_b_in_unit>TONNES</total_b_in_unit>
<loc_id>5</loc_id>
<loc_desc>Taumatawhakatangihangakoauauotamateaturipukakapikimaungahoronukupokaiwhenuakitanatahu</loc_desc>
<company_id>5010</company_id>
<company_name>Altitude Fulle Coverage</company_name>
</item>
<item>
<data>TEST3</data>
<data_datetime>20120901000000</data_datetime>
<total_a>1241.00</total_a>
<total_a_in_unit>TONNES</total_a_in_unit>
<total_b>1021.00</total_b>
<total_b_in_unit>TONNES</total_b_in_unit>
<loc_id>3</loc_id>
<loc_desc>Chargoggagoggmanchauggagoggchaubunagungamaugg</loc_desc>
<company_id>5111</company_id>
<company_name>Big Beeing Craze Food Mania</company_name>
</item>
<item>
<data>TEST5</data>
<data_datetime>20120901000000</data_datetime>
<total_a>550.00</total_a>
<total_a_in_unit>TONNES</total_a_in_unit>
<total_b>0.00</total_b>
<total_b_in_unit>TONNES</total_b_in_unit>
<loc_id>0</loc_id>
<loc_desc>Tweebuffelsmeteenskootmorsdoodgeskietfontein</loc_desc>
<company_id>73</company_id>
<company_name>Monotone Classic Paper Producer</company_name>
</item>
<item>
<data>TEST6</data>
<data_datetime>20120901000000</data_datetime>
<total_a>3664.00</total_a>
<total_a_in_unit>TONNES</total_a_in_unit>
<total_b>3450.00</total_b>
<total_b_in_unit>TONNES</total_b_in_unit>
<loc_id>6</loc_id>
<loc_desc>Pekwachnamaykoskwaskwaypinwanik</loc_desc>
<company_id>104</company_id>
<company_name>No Name Futuristic Para</company_name>
</item>
</items>
</test>
&#13;
Java应用程序
package question2_try1;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class ProcessXML {
public static void main (String[] args) {
try {
File fXmlFile = new File("C:\\Users\\User\\Desktop\\ST eletronics\\Q2_20170206112222.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());
//controlsummary & items
System.out.println("Control summary :" +doc.getElementsByTagName("controlsummary").item(0).getTextContent());
NodeList nList = doc.getElementsByTagName("item");
System.out.println("----------------------------");
for (int temp = 0; temp < nList.getLength(); temp++) {
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("Data : " + eElement.getElementsByTagName("data").item(0).getTextContent());
System.out.println("Data datetime : " + eElement.getElementsByTagName("data_datetime").item(0).getTextContent());
System.out.println("Total a : " + eElement.getElementsByTagName("total_a").item(0).getTextContent());
System.out.println("Total a in unit : " + eElement.getElementsByTagName("total_a_in_unit").item(0).getTextContent());
System.out.println("Total b : " + eElement.getElementsByTagName("total_b").item(0).getTextContent());
System.out.println("Total b in unit : " + eElement.getElementsByTagName("total_b_in_unit").item(0).getTextContent());
System.out.println("Loc id : " + eElement.getElementsByTagName("loc_id").item(0).getTextContent());
System.out.println("Loc desc : " + eElement.getElementsByTagName("loc_desc").item(0).getTextContent());
System.out.println("Company id : " + eElement.getElementsByTagName("company_id").item(0).getTextContent());
System.out.println("Company Name : " + eElement.getElementsByTagName("company_name").item(0).getTextContent());
}
}
}catch (Exception e) {
e.printStackTrace();
}
}
}
&#13;
Console:
Root element :test
Control summary :
5
----------------------------
Current Element :item
Data : TEST1
Data datetime : 20120901000000
Total a : 492.00
Total a in unit : TONNES
Total b : 420.00
Total b in unit : TONNES
Loc id : 1
Loc desc : Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch
Company id : 6001
Company Name : Great Real Estate pte ltd
Current Element :item
Data : TEST7
Data datetime : 20120901000000
Total a : 2228.00
Total a in unit : TONNES
Total b : 2260.00
Total b in unit : TONNES
Loc id : 5
Loc desc : Taumatawhakatangihangakoauauotamateaturipukakapikimaungahoronukupokaiwhenuakitanatahu
Company id : 5010
Company Name : Altitude Fulle Coverage
Current Element :item
Data : TEST3
Data datetime : 20120901000000
Total a : 1241.00
Total a in unit : TONNES
Total b : 1021.00
Total b in unit : TONNES
Loc id : 3
Loc desc : Chargoggagoggmanchauggagoggchaubunagungamaugg
Company id : 5111
Company Name : Big Beeing Craze Food Mania
Current Element :item
Data : TEST5
Data datetime : 20120901000000
Total a : 550.00
Total a in unit : TONNES
Total b : 0.00
Total b in unit : TONNES
Loc id : 0
Loc desc : Tweebuffelsmeteenskootmorsdoodgeskietfontein
Company id : 73
Company Name : Monotone Classic Paper Producer
Current Element :item
Data : TEST6
Data datetime : 20120901000000
Total a : 3664.00
Total a in unit : TONNES
Total b : 3450.00
Total b in unit : TONNES
Loc id : 6
Loc desc : Pekwachnamaykoskwaskwaypinwanik
Company id : 104
Company Name : No Name Futuristic Para
&#13;
答案 0 :(得分:0)
对于您的第一个问题 - 您可以直接使用totalRecords标记,它应该可以正常工作。
System.out.println("Control summary :" +doc.getElementsByTagName("totalrecords").item(0).getTextContent());
对于你的第二个问题 - items只是一个item(子元素)列表。 Test是你的根标签(正如你已经提到的),而items只是另一个有多个子元素的标签。您可以拥有多个不同的父标记,这些标记可以包含多个子元素。
我正在更新你的XML,最后添加一个新的标签价格给你一个例子。它包含两个子元素的价格。我还更新了代码来读取块。该代码还包含您第一个问题的答案。
<?xml version="1.0" encoding="utf-8"?>
<test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.stee.com.sg/q2.xsd">
<controlsummary>
<totalrecords>5</totalrecords>
</controlsummary>
<items>
<item>
<data>TEST1</data>
<data_datetime>20120901000000</data_datetime>
<total_a>492.00</total_a>
<total_a_in_unit>TONNES</total_a_in_unit>
<total_b>420.00</total_b>
<total_b_in_unit>TONNES</total_b_in_unit>
<loc_id>1</loc_id>
<loc_desc>Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch</loc_desc>
<company_id>6001</company_id>
<company_name>Great Real Estate pte ltd</company_name>
</item>
<item>
<data>TEST7</data>
<data_datetime>20120901000000</data_datetime>
<total_a>2228.00</total_a>
<total_a_in_unit>TONNES</total_a_in_unit>
<total_b>2260.00</total_b>
<total_b_in_unit>TONNES</total_b_in_unit>
<loc_id>5</loc_id>
<loc_desc>Taumatawhakatangihangakoauauotamateaturipukakapikimaungahoronukupokaiwhenuakitanatahu</loc_desc>
<company_id>5010</company_id>
<company_name>Altitude Fulle Coverage</company_name>
</item>
<item>
<data>TEST3</data>
<data_datetime>20120901000000</data_datetime>
<total_a>1241.00</total_a>
<total_a_in_unit>TONNES</total_a_in_unit>
<total_b>1021.00</total_b>
<total_b_in_unit>TONNES</total_b_in_unit>
<loc_id>3</loc_id>
<loc_desc>Chargoggagoggmanchauggagoggchaubunagungamaugg</loc_desc>
<company_id>5111</company_id>
<company_name>Big Beeing Craze Food Mania</company_name>
</item>
<item>
<data>TEST5</data>
<data_datetime>20120901000000</data_datetime>
<total_a>550.00</total_a>
<total_a_in_unit>TONNES</total_a_in_unit>
<total_b>0.00</total_b>
<total_b_in_unit>TONNES</total_b_in_unit>
<loc_id>0</loc_id>
<loc_desc>Tweebuffelsmeteenskootmorsdoodgeskietfontein</loc_desc>
<company_id>73</company_id>
<company_name>Monotone Classic Paper Producer</company_name>
</item>
<item>
<data>TEST6</data>
<data_datetime>20120901000000</data_datetime>
<total_a>3664.00</total_a>
<total_a_in_unit>TONNES</total_a_in_unit>
<total_b>3450.00</total_b>
<total_b_in_unit>TONNES</total_b_in_unit>
<loc_id>6</loc_id>
<loc_desc>Pekwachnamaykoskwaskwaypinwanik</loc_desc>
<company_id>104</company_id>
<company_name>No Name Futuristic Para</company_name>
</item>
</items>
<prices>
<price>
<data>TEST5</data>
<unitPrice>5</unitPrice>
</price>
<price>
<data>TEST7</data>
<unitPrice>15</unitPrice>
</price>
</prices>
</test>
&#13;
更新代码
package question2_try1;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class ProcessXML {
public static void main (String[] args) {
try {
File fXmlFile = new File("C:\\Users\\User\\Desktop\\ST eletronics\\Q2_20170206112222.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());
//controlsummary & items
System.out.println("Control summary :" +doc.getElementsByTagName("totalrecords").item(0).getTextContent());
NodeList nList = doc.getElementsByTagName("item");
System.out.println("----------------------------");
for (int temp = 0; temp < nList.getLength(); temp++) {
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("Data : " + eElement.getElementsByTagName("data").item(0).getTextContent());
System.out.println("Data datetime : " + eElement.getElementsByTagName("data_datetime").item(0).getTextContent());
System.out.println("Total a : " + eElement.getElementsByTagName("total_a").item(0).getTextContent());
System.out.println("Total a in unit : " + eElement.getElementsByTagName("total_a_in_unit").item(0).getTextContent());
System.out.println("Total b : " + eElement.getElementsByTagName("total_b").item(0).getTextContent());
System.out.println("Total b in unit : " + eElement.getElementsByTagName("total_b_in_unit").item(0).getTextContent());
System.out.println("Loc id : " + eElement.getElementsByTagName("loc_id").item(0).getTextContent());
System.out.println("Loc desc : " + eElement.getElementsByTagName("loc_desc").item(0).getTextContent());
System.out.println("Company id : " + eElement.getElementsByTagName("company_id").item(0).getTextContent());
System.out.println("Company Name : " + eElement.getElementsByTagName("company_name").item(0).getTextContent());
}
}
NodeList npricesList = doc.getElementsByTagName("price");
System.out.println("============[- _ -]======================");
for (int temp = 0; temp < npricesList.getLength(); temp++) {
Node nNode = npricesList.item(temp);
System.out.println("\nCurrent Element :" + nNode.getNodeName());
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
System.out.println("Data : " + eElement.getElementsByTagName("data").item(0).getTextContent());
System.out.println("price : " + eElement.getElementsByTagName("unitPrice").item(0).getTextContent());
}
}
}catch (Exception e) {
e.printStackTrace();
}
}
}
&#13;