求教:xml schema定义子元素
xml文件如下:
<relationship type="relationship_Products" export="true">
<attribute type="type_Model" name="subObject"/>
<attribute type="false" name="checkFile"/>
<attribute type="true" name="exportSubObject"/>
<attribute type="String" name="attribute_FeatureAllocationType"/>
<attribute type="false" name="getTo"/>
<attribute type="true" name="getFrom"/>
</relationship>
attribute没有顺序要求,出现至少五次,name值为subObject,checkFile,exportSubObject,getTo,getFrom的必须出现一次,name值为attribute_$$$$$$$$的可以有零或多个,这个schema应该怎么定义呢?
thanks!
--------------------编程问答-------------------- 尽快给点帮助吧 --------------------编程问答-------------------- package com.chliang.xml;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class XMLTest {
private Document document;
private String filename;
public XMLTest(String name) throws ParserConfigurationException{
filename=name;
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
document=builder.newDocument();
}
public void toWrite(List<String> value,int num){
Element root=document.createElement("relationship");
root.setAttribute("type", "relationship_Products");
root.setAttribute("export", "true");
document.appendChild(root);
Element attribute=document.createElement("attribute");
attribute.setAttribute("name", "subObject");
attribute.setAttribute("type", "type_Model");
root.appendChild(attribute);
Element attribute1=document.createElement("attribute");
attribute1.setAttribute("name", "checkFile");
attribute1.setAttribute("type", "false");
root.appendChild(attribute1);
Element attribute2=document.createElement("attribute");
attribute2.setAttribute("name", "exportSubObject");
attribute2.setAttribute("type", "true");
root.appendChild(attribute2);
Element attribute3=document.createElement("attribute");
attribute3.setAttribute("name", "getTo");
attribute3.setAttribute("type", "false");
root.appendChild(attribute3);
Element attribute4=document.createElement("attribute");
attribute4.setAttribute("name", "getFrom");
attribute4.setAttribute("type", "true");
root.appendChild(attribute4);
for(int i = 0;i < num;i++){
Element attribute5=document.createElement("attribute");
attribute5.setAttribute("name", "attribute_"+value.get(i));
attribute5.setAttribute("type", "String");
root.appendChild(attribute5);
}
}
public void toSave(){
try{
TransformerFactory tf=TransformerFactory.newInstance();
Transformer transformer=tf.newTransformer();
DOMSource source=new DOMSource(document);
transformer.setOutputProperty(OutputKeys.ENCODING,"GB2312");
transformer.setOutputProperty(OutputKeys.INDENT,"yes");
PrintWriter pw=new PrintWriter(new FileOutputStream(filename));
StreamResult result=new StreamResult(pw);
transformer.transform(source,result);
}
catch(TransformerException mye){
mye.printStackTrace();
}
catch(IOException exp){
exp.printStackTrace();
}
}
public static void main(String args[]){
try{
XMLTest myxml=new XMLTest("C:/资料/IBM/6.xml");
List<String> value = new ArrayList<String>();
value.add("123");
value.add("456");
myxml.toWrite(value,2);
myxml.toSave();
System.out.print("successful!");
}
catch(ParserConfigurationException exp){
exp.printStackTrace();
System.out.print("failed!");
}
}
}
--------------------编程问答-------------------- 你的文档中type 和name是attribute的属性吗 --------------------编程问答--------------------
attribute是子元素的名称,都是一样的,所以我就不知道该怎么写了,type和name是每个attribute的属性
补充:Java , Java SE