2 回答
TA贡献1725条经验 获得超7个赞
我不得不尝试另一种方法并正确解析XML
如果你可以在这里使用外部库是如何:
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
File stocks = new File("PhillPlugin.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(stocks);
doc.getDocumentElement().normalize();
NodeList nodes = doc.getElementsByTagName("AttributeValuePair");
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node;
if(i==0)
{
tempurlGen=getValue("value",element);
System.out.println("GenerateUrl: " + getValue("value", element));
}
else if (i==1)
{
tempurlVal=getValue("value",element);
System.out.println("ValidateUrl: " + getValue("value", element));
}
}
}
}
static String getValue(String tag, Element element) {
NodeList nodes = element.getElementsByTagName(tag).item(0).getChildNodes();
Node node = (Node) nodes.item(0);
return node.getNodeValue();
}
如果你不能javax在这里包含库是如何解析XML使用流
public void getXMLData() throws Exception {
File stocks = new File("PhillPlugin.xml");
BufferedInputStream in = new BufferedInputStream(new FileInputStream(stocks));
StringBuilder sb = new StringBuilder();
int cp;
while ((cp = in.read()) != -1) {
sb.append((char) cp);
String t = sb.toString();
if (t.contains("</AttributeValuePair>")) {
String test = sb.toString();
String test1p[] = test.split("<value>|</value>");
tempurlGen = test1p[1];
break;
}
}
sb = new StringBuilder();
while ((cp = in.read()) != -1) {
sb.append((char) cp);
String t = sb.toString();
if (t.contains("</AttributeValuePair>")) {
String test = sb.toString();
String test1p[] = test.split("<value>|</value>");
tempurlVal = test1p[1];
break;
}
}
}
确保你定义tempurlGen和tempurlVal
TA贡献1895条经验 获得超7个赞
尝试这个:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"author",
"email",
"creationDate",
"description",
"configuration"
})
@XmlRootElement(name = "Plugin")
public class Plugin {
//Add getters and setters including ones for inner classes
@XmlElement(required = true)
private String author;
@XmlElement(required = true)
private String email;
@XmlElement(required = true)
private String creationDate;
@XmlElement(required = true)
private String description;
@XmlElement(required = true)
private Plugin.Configuration configuration;
@XmlAttribute(name = "type")
private String type;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"attributeValuePair"
})
public static class Configuration {
@XmlElement(name = "AttributeValuePair", required = true)
private Plugin.Configuration.AttributeValuePair attributeValuePair;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"attribute",
"mandatory",
"instanceOverride",
"globalUIOverride",
"value"
})
public static class AttributeValuePair {
@XmlElement(name = "Attribute", required = true)
private Plugin.Configuration.AttributeValuePair.Attribute attribute;
@XmlElement(required = true)
private String mandatory;
@XmlElement(required = true)
private String instanceOverride;
@XmlElement(required = true)
private String globalUIOverride;
@XmlElement(required = true)
private String value;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"value"
})
public static class Attribute {
@XmlValue
private String value;
@XmlAttribute(name = "type")
private String type;
@XmlAttribute(name = "length")
private Byte length;
}
}
}
}
对于解组部分:
JAXBContext jaxbContext = JAXBContext.newInstance(Plugin.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
StringReader reader = new StringReader("xml string here");
Plugin plugin = (Plugin) unmarshaller.unmarshal(reader);
添加回答
举报