如何在Java中读取包含多组元素且没有属性值的XML文件?
我主要想知道如何从xml中获取这些元素值 java中的文件将其设置到对象中 下面是示例XML文件如何在Java中读取包含多组元素且没有属性值的XML文件?,java,xml,xml-parsing,Java,Xml,Xml Parsing,我主要想知道如何从xml中获取这些元素值 java中的文件将其设置到对象中 下面是示例XML文件 <?xml version="1.0" encoding="UTF-8"?> <Response> <LoanApplication> <LoanId>1111</LoanId> <LoanStatus>Unsuccessful</LoanStatus> <LoanRe
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<LoanApplication>
<LoanId>1111</LoanId>
<LoanStatus>Unsuccessful</LoanStatus>
<LoanReference>111</LoanReference>
</LoanApplication>
<LoanApplication>
<LoanId>222</LoanId>
<LoanStatus>Unsuccessful</LoanStatus>
<LoanReference>333</LoanReference>
</LoanApplication>
<LoanApplication>
<LoanId>222</LoanId>
<LoanStatus>Unsuccessful</LoanStatus>
<LoanReference>4444</LoanReference>
</LoanApplication>
<LoanApplication>
<LoanId>555</LoanId>
<LoanStatus>Current</LoanStatus>
<LoanReference>7777</LoanReference>
</LoanApplication>
<LoanApplication>
<LoanId>3333</LoanId>
<LoanStatus>Current</LoanStatus>
<LoanReference>9999</LoanReference>
</LoanApplication>
</Response>
下面是解析xml文件的方法。现在,我被如何安排我的班主任所打动
public Set<LoanIDResponse> getLoanIDList()
{
Set<LoanIDResponse> loanIDSet = new HashSet<>();
try
{
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse(new File("Response.xml"));
doc.getDocumentElement().normalize();
NodeList listOfLoanApplication = doc.getElementsByTagName("LoanApplication");
int totalLoanApplication = listOfLoanApplication.getLength();
for (int s = 0; s < listOfLoanApplication.getLength(); s++)
{
Node loanApplicationNode = listOfLoanApplication.item(s);
if (loanApplicationNode.getNodeType() == Node.ELEMENT_NODE)
{
LoanIDResponse response = new LoanIDResponse();
Element loanApplicationElement = (Element) loanApplicationNode;
NodeList loanIDList = loanApplicationElement.getElementsByTagName("LoanId");
NodeList loanReferenceList = loanApplicationElement.getElementsByTagName("LoanReference");
NodeList loanStatusList = loanApplicationElement.getElementsByTagName("LoanStatus");
}
}
} catch (SAXParseException err)
{
LOGGER.log(Level.WARNING, "** Parsing error" + ", line {0}, uri {1}", new Object[]
{
err.getLineNumber(), err.getSystemId()
});
LOGGER.log(Level.WARNING, " {0}", err.getMessage());
}
return loanIDSet;
}
public Set getLoanIDList()
{
Set loanIDSet=新HashSet();
尝试
{
DocumentBuilderFactory docBuilderFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder=docBuilderFactory.newDocumentBuilder();
documentdoc=docBuilder.parse(新文件(“Response.xml”);
doc.getDocumentElement().normalize();
NodeList ListofLanaApplication=doc.getElementsByTagName(“LoanApplication”);
int totaloanapplication=listofLanapplication.getLength();
for(int s=0;s
首先查看和了解更多详细信息
这个例子适用于JAXB
首先,您需要一些类来保存数据
贷款申请
这是贷款申请的基本属性
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "LoanApplication")
public class LoanApplication {
private long loanId;
private String loanStatus;
private long loadReference;
public long getLoanId() {
return loanId;
}
public String getLoanStatus() {
return loanStatus;
}
public long getLoadReference() {
return loadReference;
}
@XmlElement(name="LoanId")
public void setLoanId(long loanId) {
this.loanId = loanId;
}
@XmlElement(name="LoanStatus")
public void setLoanStatus(String loanStatus) {
this.loanStatus = loanStatus;
}
@XmlElement(name="LoanReference")
public void setLoadReference(long loadReference) {
this.loadReference = loadReference;
}
@Override
public String toString() {
return getLoanId() + "; " + getLoanStatus() + "; " + getLoadReference();
}
}
回答
这是一个“container”类,包含LoanApplications
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "Response")
public class Response {
private List<LoanApplication> applications;
@XmlElement(name="LoanApplication")
public void setApplications(List<LoanApplication> applications) {
this.applications = applications;
}
public List<LoanApplication> getApplications() {
return applications;
}
}
基于我的示例代码,输出
1111; Unsuccessful; 111
222; Unsuccessful; 333
222; Unsuccessful; 4444
555; Current; 7777
3333; Current; 9999
写
写作和阅读差不多,事实上,在这个例子中,它也包括阅读部分,所以我们可以更新内容
try {
File file = new File("Responses.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Response.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Response response = (Response) jaxbUnmarshaller.unmarshal(file);
for (LoanApplication app : response.getApplications()) {
System.out.println(app);
}
LoanApplication app = new LoanApplication();
app.setLoadReference(123456);
app.setLoanId(7890);
app.setLoanStatus("Approved");
response.getApplications().add(app);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(response, new File("Responses.xml"));
//jaxbMarshaller.marshal(response, System.out);
} catch (JAXBException ex) {
ex.printStackTrace();
}
这就产生了
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Response>
<LoanApplication>
<LoanReference>111</LoanReference>
<LoanId>1111</LoanId>
<LoanStatus>Unsuccessful</LoanStatus>
</LoanApplication>
<LoanApplication>
<LoanReference>333</LoanReference>
<LoanId>222</LoanId>
<LoanStatus>Unsuccessful</LoanStatus>
</LoanApplication>
<LoanApplication>
<LoanReference>4444</LoanReference>
<LoanId>222</LoanId>
<LoanStatus>Unsuccessful</LoanStatus>
</LoanApplication>
<LoanApplication>
<LoanReference>7777</LoanReference>
<LoanId>555</LoanId>
<LoanStatus>Current</LoanStatus>
</LoanApplication>
<LoanApplication>
<LoanReference>9999</LoanReference>
<LoanId>3333</LoanId>
<LoanStatus>Current</LoanStatus>
</LoanApplication>
<LoanApplication>
<LoanReference>123456</LoanReference>
<LoanId>7890</LoanId>
<LoanStatus>Approved</LoanStatus>
</LoanApplication>
</Response>
111
1111
不成功的
333
222
不成功的
4444
222
不成功的
7777
555
现在的
9999
3333
现在的
123456
7890
经核准的
注意,最后一条记录是新的
我几乎把它一瘸一拐地放在一起,你可以得到节点值,比如
NodeList loanIDList = loanApplicationElement.getElementsByTagName("LoanId");
Element loanIDElement = (Element) loanIDList.item(0);
NodeList textIDList = loanIDElement.getChildNodes();
response.setLoanID(Integer.parseInt(((Node)textIDList.item(0)).getNodeValue().trim()));
//-------
NodeList loanReferenceList = loanApplicationElement.getElementsByTagName("LoanReference");
Element loanReferenceElement = (Element) loanReferenceList.item(0);
NodeList textLRList = loanReferenceElement.getChildNodes();
response.setLoanReference(Integer.parseInt(((Node) textLRList.item(0)).getNodeValue().trim()));
//-------
NodeList loanStatusList = loanApplicationElement.getElementsByTagName("LoanStatus");
Element loanStatusElement = (Element) loanStatusList.item(0);
NodeList textLSList = loanStatusElement.getChildNodes();
response.setLoanStatus(((Node) textLSList.item(0)).getNodeValue().trim());
,请告诉我们您为实现此目标所做的努力,以及您遇到的错误。StackOverflow不是填鸭式的。现在你可以看到我的源代码了@Vishrant@Z.U.H您得到的是什么错误/异常。如何从tag@VishrantI am获得节点值,并从答案中获得深刻印象。。。优秀:)@Z.I.J在15分钟的谷歌搜索和黑客攻击中表现不错:我认为这对新开发人员来说并不容易。。感谢分享宝贵的答案和评论。。我能和你交朋友吗?@Z.I.J:)对不起,这是你唯一想去的地方。。我很难过看到你的抱歉..:P
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Response>
<LoanApplication>
<LoanReference>111</LoanReference>
<LoanId>1111</LoanId>
<LoanStatus>Unsuccessful</LoanStatus>
</LoanApplication>
<LoanApplication>
<LoanReference>333</LoanReference>
<LoanId>222</LoanId>
<LoanStatus>Unsuccessful</LoanStatus>
</LoanApplication>
<LoanApplication>
<LoanReference>4444</LoanReference>
<LoanId>222</LoanId>
<LoanStatus>Unsuccessful</LoanStatus>
</LoanApplication>
<LoanApplication>
<LoanReference>7777</LoanReference>
<LoanId>555</LoanId>
<LoanStatus>Current</LoanStatus>
</LoanApplication>
<LoanApplication>
<LoanReference>9999</LoanReference>
<LoanId>3333</LoanId>
<LoanStatus>Current</LoanStatus>
</LoanApplication>
<LoanApplication>
<LoanReference>123456</LoanReference>
<LoanId>7890</LoanId>
<LoanStatus>Approved</LoanStatus>
</LoanApplication>
</Response>
NodeList loanIDList = loanApplicationElement.getElementsByTagName("LoanId");
Element loanIDElement = (Element) loanIDList.item(0);
NodeList textIDList = loanIDElement.getChildNodes();
response.setLoanID(Integer.parseInt(((Node)textIDList.item(0)).getNodeValue().trim()));
//-------
NodeList loanReferenceList = loanApplicationElement.getElementsByTagName("LoanReference");
Element loanReferenceElement = (Element) loanReferenceList.item(0);
NodeList textLRList = loanReferenceElement.getChildNodes();
response.setLoanReference(Integer.parseInt(((Node) textLRList.item(0)).getNodeValue().trim()));
//-------
NodeList loanStatusList = loanApplicationElement.getElementsByTagName("LoanStatus");
Element loanStatusElement = (Element) loanStatusList.item(0);
NodeList textLSList = loanStatusElement.getChildNodes();
response.setLoanStatus(((Node) textLSList.item(0)).getNodeValue().trim());