-3

文件应该是这样的

<root>
     <node label="ValueFromDatabase"/>        
</root>;

并且将有更多的节点标签,我将从数据库中获取标签是可变的。

其实我应该把这个问题说得更清楚。我用这样的字符串

String xmlSourceResource =
             "<?xml version='1.0' encoding='UTF-8'?>\n"+
             "<root>\n"+
             xmlString+
             "</root>";

xmlString 变量包含带有标签的节点。现在,我将其解析为 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 文件。

不过感谢您的回答。

4

1 回答 1

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>
于 2013-07-02T14:44:00.840 回答