文件应该是这样的
<root>
<node label="ValueFromDatabase"/>
</root>;
并且我将从数据库中获取更多节点标签,标签是可变的。
实际上我应该更清楚地提出这个问题。我使用了这样的字符串
String xmlSourceResource =
"<?xml version='1.0' encoding='UTF-8'?>\n"+
"<root>\n"+
xmlString+
"</root>";
xmlString变量包含带有label.Now的节点,我正在将它解析为xml,如下所示
StringWriter xmlResultResource = new StringWriter();
Transformer xmlTransformer=TransformerFactory.newInstance().newTransformer();
xmlTransformer.transform(new StreamSource(new StringReader(xmlSourceResource)),new StreamResult(xmlResultResource));
我需要在XML变量的Flex动作脚本类中使用它。所以,如果可能的话,我应该如何解析它或将XML发送到flex。我不想制作XML文件。
感谢您的回答。
答案 0 :(得分:1)
我希望这会有所帮助:
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
/**
*
* @author ugo_dock
*/
public class JavaToXML {
public static void main(String args[]){
Myself myself = new Myself();
myself.setAge(48);
myself.setFirstName("Barry");
myself.setLastName("White");
try{
JAXBContext jAXBContext = JAXBContext.newInstance(Myself.class);
Marshaller jaxbMarshaller = jAXBContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(myself, System.out);
}
catch(JAXBException e){
e.printStackTrace(System.out);
}
}
}
@XmlRootElement
class Myself{
private String firstName;
private String lastName;
private int age;
public String getFirstName() {
return firstName;
}
@XmlElement
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
@XmlElement
public void setLastName(String lastName) {
this.lastName = lastName;
}
public int getAge() {
return age;
}
@XmlElement
public void setAge(int age) {
this.age = age;
}
}
输出将是:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<myself>
<age>48</age>
<firstName>Barry</firstName>
<lastName>White</lastName>
</myself>