gpt4 book ai didi

java - 解析xml并制作对象java

转载 作者:数据小太阳 更新时间:2023-10-29 02:50:05 26 4
gpt4 key购买 nike

找人查看我的简单代码。我对我正在做的事情很陌生,我知道我可能只是在某个地方犯了一个简单的错误。

我正在解析一个基于 http 的 xml 文件,并尝试将与元素关联的文本打印到屏幕上,并创建一个由这些元素中的文本填充的对象。

我可以打印所有的元素和关联的文本,但对象的字段中都具有空值。让我知道是否需要更好地解释。

代码如下:

项目等级:

   package com.entities;



public class StageOfLife
{

private String endDate;
private String offerData;
private String offerType;
private String redemption;
private String startDate;
private String termsConditions;
private String title;
private String merchantDescription;
private String merchantLogo;
private String merchantName;

public StageOfLife() {

}

public StageOfLife(String endDate, String offerData, String offerType,
String redemption, String startDate, String termsConditions,
String title, String merchantDescription, String merchantLogo,
String merchantName)
{

// Getters Setters HEre

public String toString() {

StringBuffer buffer = new StringBuffer();

buffer.append(endDate);
buffer.append("\n");
buffer.append(offerData);
buffer.append("\n");
buffer.append(offerType);
buffer.append("\n");
buffer.append(redemption);
buffer.append("\n");
buffer.append(startDate);
buffer.append("\n");
buffer.append(termsConditions);
buffer.append("\n");
buffer.append(title);
buffer.append("\n");
buffer.append(merchantDescription);
buffer.append("\n");
buffer.append(merchantLogo);
buffer.append("\n");
buffer.append(merchantName);

return buffer.toString();
}

}

这是包含方法和主要内容的类:

package com.xmlparse;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import com.entities.StageOfLife;



public class XmlParserStage
{
Document dom;
DocumentBuilder db;
List<StageOfLife> myStageList;

public XmlParserStage(){
//create a list to hold the StageOfLife objects
myStageList = new ArrayList<StageOfLife>();
}


private void parseXmlFile(){
//get the factory
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

try {

//Using factory get an instance of document builder
db = dbf.newDocumentBuilder();

//parse to get DOM representation of the XML file
dom = db.parse("http:/url/goes/here");


} catch(ParserConfigurationException pce) {
pce.printStackTrace();
} catch(SAXException se) {
se.printStackTrace();
} catch(IOException ioe) {
ioe.printStackTrace();
}
}

private void parseDocument() {


//get the root element
Element docEle = dom.getDocumentElement();

//get a nodelist of elements
NodeList nl = docEle.getElementsByTagName("Offer");
if(nl != null && nl.getLength() > 0) {
for(int i = 0 ; i < nl.getLength(); i++) {

//get the elements
Element solEl = (Element)nl.item(i);

//get the StageOfLife object
StageOfLife sol = getStageOfLife(solEl);

//add it to list
myStageList.add(sol);
}
}
}

/*
take an <offer> element and read the values in, create
an StageOfLife object and return it
*/
private StageOfLife getStageOfLife(Element solEl) {

/*
for each <offer> element get the values
*/

String endDate = getTextValue(solEl,"EndDate");
String offerData = getTextValue(solEl,"OfferData");
String offerType = getTextValue(solEl,"OfferType");
String redemption = getTextValue(solEl,"Redemption");
String startDate = getTextValue(solEl,"StartDate");
String termsConditions = getTextValue(solEl,"TermsConditions");
String title = getTextValue(solEl,"Title");
String merchantDescription = getTextValue(solEl,"MerchantDescription");
String merchantLogo = getTextValue(solEl,"MerchantLogo");
String merchantName = getTextValue(solEl,"MerchantName");

//Create a new StageOfLife object with the value read from the xml nodes
StageOfLife sol = new StageOfLife(endDate, offerData, offerType,
redemption, startDate, termsConditions,
title, merchantDescription, merchantLogo,
merchantName);

return sol;
}

/*
take a xml element and the tag name, look for the tag and get
the text content
*/

private String getTextValue(Element ele, String tagName) {

String textVal = null;
NodeList nl = ele.getElementsByTagName(tagName);
if(nl != null && nl.getLength() > 0) {
Element el = (Element)nl.item(0);
textVal = el.getFirstChild().getNodeValue();

System.out.print(el + ":" + textVal);
System.out.println();

}

return textVal;
}


/*
Calls getTextValue and returns a int value
*/
private int getIntValue(Element ele, String tagName) {

return Integer.parseInt(getTextValue(ele,tagName));
}

private void printData(){

System.out.println("Number of Offers: '" + myStageList.size() + "'.");

Iterator it = myStageList.iterator();
while(it.hasNext()) {
System.out.println(it.next().toString());

}
}

public void run() {

//parse the xml file and get the dom object
parseXmlFile();

//get each stageoflife element and create a StageOfLife object
parseDocument();

//Iterate through the list and print the data
printData();
}

public static void main(String [] args) throws ClientProtocolException, IOException {

XmlParserStage xmlParser = new XmlParserStage();

xmlParser.httpClient();

xmlParser.run();

}

}

最佳答案

你的构造函数没有做任何事情!

public StageOfLife(String endDate, String offerData, String offerType,
String redemption, String startDate, String termsConditions,
String title, String merchantDescription, String merchantLogo,
String merchantName)
{
// set the data
this.endDate = endDate;
...
}

但更好的方法是使用 Java XML Binding jaxb。它自动将 java 类映射到 xml

关于java - 解析xml并制作对象java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13957677/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com