gpt4 book ai didi

Java - SAXParser 和 XMLReader 获取 null 属性值

转载 作者:行者123 更新时间:2023-12-01 10:09:39 25 4
gpt4 key购买 nike

我有一个 XML 文件,我想使用 SAX 从中获取数据。它一直持续到 startElement() 为止,出现错误,因为当我尝试获取属性值时,这些属性值为 null。为什么?

首先,我称这个方法为:load()

public RealEstate load() {
RealEstate data = null;
try {
//Create a "parser factory" for creating SAX parsers
SAXParserFactory spfac = SAXParserFactory.newInstance();

//Now use the parser factory to create a SAXParser object
SAXParser sp = spfac.newSAXParser();
XMLReader xmlReader = sp.getXMLReader();

//Create an instance of this class; it defines all the handler methods
SaxParserRealEstateHandler handler = new SaxParserRealEstateHandler();

//assign our handler
xmlReader.setContentHandler(handler);

// Convert file to URL.
URL url = handler.fileToURL(new File(xmlFilename));

// Parse file.
xmlReader.parse(url.toString());

data = handler.getData();

} catch (ParserConfigurationException ex) {
Logger.getLogger(RealStatePersistXML.class.getName()).log(Level.SEVERE, null, ex);
}
catch (SAXException ex) {
Logger.getLogger(RealStatePersistXML.class.getName()).log(Level.SEVERE, null, ex);
}
catch (IOException ex) {
Logger.getLogger(RealStatePersistXML.class.getName()).log(Level.SEVERE, null, ex);
}
return data;
}

这里有方法startElementendElement

    class SaxParserRealEstateHandler extends DefaultHandler
{

private String temp;
private RealEstate data;
private Estate estate;
private estateAddress address;

/**
* CONSTRUCTOR
*/
public SaxParserRealEstateHandler()
{
}


/**
* getData()
* This method gets the list of estates from RealEstate class.
* @return data list of states. Null in case of error or list not found.
*/
public RealEstate getData()
{
// RealEstate data = new RealEstate();
// data.addEstate(estate);
// data.getEstates();
// return data;
}

/*
* When the parser encounters plain text (not XML elements),
* it calls(this method, which accumulates them in a string buffer
*/
public void characters(char[] buffer, int start, int length) {
temp = new String(buffer, start, length);
}


/*
* Every time the parser encounters the beginning of a new element,
* it calls this method, which resets the string buffer
*/
public void startElement(String uri, String localName, String qName, Attributes attributes){

switch (qName){
case"realState":
data = new RealEstate();
break;
case"estate":
estate = new Estate();
estate.setType(attributes.getValue("type"));
estate.setSurface(Double.parseDouble(attributes.getValue("surface")));
//estate.setAddress(attributes.getValue("address"));
estate.setPrice(Integer.parseInt(attributes.getValue("price")));
break;
case"address":
address = new estateAddress();
address.setStreet(attributes.getValue("type"));
address.setNumber(Integer.parseInt(attributes.getValue("surface")));
address.setFloor(Integer.parseInt(attributes.getValue("type")));
address.setDoor(Integer.parseInt(attributes.getValue("surface")));
break;
default:
break;
}
}



public void endElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {

switch (qName){
case"estate":
data.getEstates().add(estate);//quant ha llegit la etiqueta (</estate>) l'agreguem
break;
case"surface":
double surface = Double.parseDouble(temp);
estate.setSurface(surface);
break;
case"price":
double price = Double.parseDouble(temp);//tractar tema errors del parseDouble!
estate.setPrice(price);
break;
case"address":
estate.setAddress(address);
break;
case"street":
address.setStreet(temp);
break;
case"number":
int number = Integer.parseInt(temp);
address.setNumber(number);
break;
case"floor":
address.setStreet(temp);
break;
case"door":
address.setStreet(temp);
break;
default:
break;
}
}



/** fileToURL()
* Convenience method to convert a file to a url
* @throws Error if malformed url exception is generated
*/

public URL fileToURL(File file)
{
String path = file.getAbsolutePath();
String fSep = System.getProperty("file.separator");
if (fSep != null && fSep.length() == 1)
path = path.replace(fSep.charAt(0), '/');
if (path.length() > 0 && path.charAt(0) != '/')
path = '/' + path;
try
{
return new URL("file", null, path);
}
catch (java.net.MalformedURLException e)
{
throw new Error("Unexpected MalformedURLException");//no he pogut muntar una url com deu mana
//retornar null
}
}
}

XML 代码

<?xml version="1.0" encoding="UTF-8"?>
<realState xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="realState.xsd">
<estate>
<type>House</type>
<surface>250,0</surface>
<address>
<street>C/Tarragona</street>
<number>54</number>
<floor>2</floor>
<door>1</door>
</address>
<price>140000,0</price>
</estate>
</realState>

XSD 代码:http://pastebin.com/nzEDsdLg

类 RealEstate 和 Estate 工作正常!

谢谢!

最佳答案

type , surface ,和price不是 <estate>属性元素。它们是子元素。

作为属性,您的 XML 应该是:

<estate type="House" surface="250,0" price="140000,0">
<address>
<street>C/Tarragona</street>
<number>54</number>
<floor>2</floor>
<door>1</door>
</address>
</estate>

使用 StAX 解析器比 SAX 解析器容易得多,并且具有非常相似的性能特征,因此我建议您改用它。

关于Java - SAXParser 和 XMLReader 获取 null 属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36211271/

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