是否有任何Java API在运行时使用输入xml架构和xpath表达式和数据的键值对生成xml

时间:2013-05-16 05:31:37

标签: java xml xpath

我在运行时有一个可更改的xml架构文件。我在我的代码中使用XPath表达式获取java Collection,并在XML文件中输入值。在所有这些运行时输入的帮助下,我必须生成XML文件。     下面附有样本输入和输出。

  1. 以下是示例架构(无固定格式):

        <xs:complexType name="root">
            <xs:sequence>
                <xs:element name="top" type="topType" />
            </xs:sequence>
       </xs:complexType>            
       <xs:element name="root" type="root">
       </xs:element>              
       <xs:complexType name="topType">
          <xs:element name="mode" use="required" />
           <xs:element name="address" use="required" />
       </xs:complexType>
    
  2. xpath Expression&amp;我得到的值是来自Hash Map的键值对。我需要将这些xpath值与output.xml中的相应XMLElement值一起放置。 xpath表达式和值如下:

    1. 表达式:/ root / top / address值:10.200.111。
    2. 表达式:/ root / top / mode值:cluster
  3. java代码应该生成XML文件作为Output.xml :(这是需要在运行时从所有收集的输入生成的虚拟文件)

    <root>
        <top>
            <mode>cluster</mode>
            <address>10.200.111.111</address>
        </top>
    </root>
    
  4. 请建议是否有人遇到过这种情况。

    提前致谢。

2 个答案:

答案 0 :(得分:1)

请查看以下代码,一旦生成使用XMLDocument,XPath生成xml,更新/添加字段

import javax.xml.transform.stream.StreamResult;

import jlibs.xml.sax.XMLDocument;
import jlibs.xml.xsd.XSInstance;
import jlibs.xml.xsd.XSParser;

import org.apache.xerces.xs.XSModel;

import javax.xml.namespace.QName;

public class XSDToXML {

    /**
     * @param args
     * @throws ClassCastException
     * @throws IllegalAccessException
     * @throws InstantiationException
     * @throws ClassNotFoundException
     */
    public static void main(String[] args) throws ClassNotFoundException,
            InstantiationException, IllegalAccessException, ClassCastException {
        try {

            XSModel model = (XSModel) new XSParser().parse("c:\\kar\\xs.xsd");
            XMLDocument sample = new XMLDocument(new StreamResult(
                "c:\\kar\\root3.xml"), false, 4, null);
                        QName root = new QName("root");
            XSInstance instance = new XSInstance();
            instance.minimumElementsGenerated = 0;
            instance.maximumElementsGenerated = 0;
            instance.generateDefaultAttributes = true;
            instance.generateOptionalAttributes = true;
            instance.maximumRecursionDepth = 0;
            instance.generateOptionalElements = true;
            instance.generate(model, root, sample);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

}

你需要下载jlibs-xml.jar,xercesImpl.jar和jlibs-core.jar文件

答案 1 :(得分:0)

请查看此代码非常简单

public class MyTestClass {

    public static void main(String[] args) {
        try{
        Map map= new HashMap<String,String>();
        map.put("cluster", "10.200.111.111");
        map.put("cluster1", "10.200.121.111");

        MyXML xml = new MyXML();
        List<Top> top1= new ArrayList<Top>();
        Set<String> keys = map.keySet();
        for(String key : keys){
            Top top=new Top();
            top.setMode(key);
            top.setAddress((String)map.get(key));
            top1.add(top);
        }
        xml.setTop(top1);
        File file = new File("C:\\kar\\file.xml");
        JAXBContext jaxbContext = JAXBContext.newInstance(MyXML.class);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

        // output pretty printed
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        jaxbMarshaller.marshal(xml, file);
        jaxbMarshaller.marshal(xml, System.out);
        }catch(Exception ex){
            System.out.println(ex.getMessage());
        }
    }
}


@XmlRootElement(name="top")
@XmlType(name="top")
@XmlAccessorType(XmlAccessType.FIELD)
public class Top {

    @XmlElement(name="mode", required=true)
    private String mode;

    @XmlElement(name="mode", required=true)
    private String address;

    public String getMode() {
        return mode;
    }

    public void setMode(String mode) {
        this.mode = mode;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }




}





@XmlRootElement(name="root")
@XmlType(name="root")
@XmlAccessorType(XmlAccessType.FIELD)
public class MyXML {


    @XmlElement(name="Top")
    private List<Top> top;

    public List<Top> getTop() {
        return top;
    }

    public void setTop(List<Top> top) {
        this.top = top;
    }

}

您甚至可以使用XSLT http://www.ictforu.com/index.php/Core-Java/java-xslt.html

来完成