자바 옮 겨 다 니 며 xml 파일 내용 읽 기
package test;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Iterator;
import javax.xml.namespace.NamespaceContext;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMAttribute;
import org.apache.axiom.om.OMComment;
import org.apache.axiom.om.OMContainer;
import org.apache.axiom.om.OMDataSource;
import org.apache.axiom.om.OMDocType;
import org.apache.axiom.om.OMDocument;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMException;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.om.OMProcessingInstruction;
import org.apache.axiom.om.OMSourcedElement;
import org.apache.axiom.om.OMText;
import org.apache.axiom.om.OMXMLParserWrapper;
import org.apache.axiom.om.impl.builder.StAXOMBuilder;
import org.xml.sax.helpers.XMLReaderFactory;
public class Axiomtest {
public static void main(String[] args) throws FileNotFoundException, Throwable {
// read xml
FileInputStream xmlFile = new FileInputStream("line-item2.xml");
XMLStreamReader parser = XMLInputFactory.newInstance().createXMLStreamReader(xmlFile);
// StAXOMBuilder
StAXOMBuilder builder = new StAXOMBuilder(parser);
OMElement doc = builder.getDocumentElement(); // <fool></fool>
OMElement cre = doc.getFirstChildWithName(new QName("student")); // <student>
OMElement cre1 = cre.getFirstChildWithName(new QName("id")); // <id></id>
System.out.println(cre1.getLocalName()+":"+cre1.getText());
cre1 = cre.getFirstChildWithName(new QName("name")); // <name></name>
System.out.println(cre1.getLocalName()+":"+cre1.getText());
cre1 = cre.getFirstChildWithName(new QName("age")); // <age></age>
System.out.println(cre1.getLocalName()+":"+cre1.getText());
cre1 = cre.getFirstChildWithName(new QName("sex")); // <sex></sex>
System.out.println(cre1.getLocalName()+":"+cre1.getText());
cre1 = cre.getFirstChildWithName(new QName("message")); // <sex></sex>
System.out.println(cre1.getLocalName()+":"+cre1.getText());
System.out.println("------------------------------1");
Iterator<OMElement> iter = doc.getChildElements();
while(iter.hasNext()){
OMElement temp = iter.next();
System.out.println("====================");
System.out.println(temp.getLocalName());
// System.out.println(temp.getText());
if(temp.getLocalName().equals("student")){
Iterator<OMElement> iter1 = temp.getChildElements();
System.out.println("----------------");
while(iter1.hasNext()){
OMElement temp1 = iter1.next();
System.out.println(temp1.getLocalName()+":"+temp1.getText());
}
}
}
System.out.println("!!!!!!!!!!!!!");
FileInputStream file = new FileInputStream("line-item2.xml");
XMLStreamReader read = XMLInputFactory.newInstance().createXMLStreamReader(file);
StAXOMBuilder sta = new StAXOMBuilder(read);
OMElement all = sta.getDocumentElement();
Iterator<OMElement> ite1 = all.getChildElements();
while(ite1.hasNext()){
OMElement temp = ite1.next();
if(temp.getLocalName().equals("student")){
Iterator<OMElement> ite2 = temp.getChildElements();
while(ite2.hasNext()){
OMElement temp1 = ite2.next();
System.out.println(temp1.getLocalName()+":"+temp1.getText());
}
}
}
// write xml
OMFactory factory = OMAbstractFactory.getOMFactory();
// doc ,doc root
OMDocument dod = factory.createOMDocument();
// root
OMElement root = factory.createOMElement("root","","");
OMElement add = factory.createOMElement("dabi","","");
//
OMElement stu = factory.createOMElement("student","","");
stu.addChild(factory.createOMText("mac"));
OMElement tea = factory.createOMElement("teacher","","");
tea.addChild(factory.createOMText("silly"));
// , root
root.addChild(stu);
root.addChild(tea);
// , root doc
dod.addChild(root);
// writer
XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(
new FileOutputStream("2.xml"));
root.serialize(writer); // cache on
writer.flush();
FileInputStream xmlFile1 = new FileInputStream("2.xml");
XMLStreamReader parser1 = XMLInputFactory.newInstance().createXMLStreamReader(xmlFile1);
StAXOMBuilder builder1 = new StAXOMBuilder(parser1);
OMElement doc1 = builder1.getDocumentElement();
Iterator<OMElement> iter1 = doc1.getChildElements();
while(iter1.hasNext()){
OMElement temp = iter1.next();
System.out.println("====================");
System.out.println(temp.getLocalName()+":"+temp.getText());
}
System.out.println("!!!!!!!!");
OMFactory omf = OMAbstractFactory.getOMFactory();
// OMDocument od = omf.createOMDocument();
OMElement root1 = omf.createOMElement("root","","");
OMElement name = omf.createOMElement("name","","");
OMElement sex = omf.createOMElement("sexy","","");
sex.addChild(omf.createOMText("man"));
name.addChild(omf.createOMText("dabi"));
root1.addChild(sex);
root1.addChild(name);
// od.addChild(root1);
XMLStreamWriter xmlw = XMLOutputFactory.newInstance().createXMLStreamWriter(new FileOutputStream("3.xml"));
root1.serialize(xmlw);
xmlw.flush();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<fool>
<student>
<name>mac</name>
<id>12</id>
<age>33</age>
<sex>male</sex>
<message>hello world</message>
</student>
<student>
<name>silly</name>
<id>5</id>
<age>12</age>
<sex>female</sex>
</student>
<teacher>
<name>Mr. Jones</name>
<id>2</id>
<age>31</age>
<sex>male</sex>
</teacher>
<student>
<name>macy</name>
<id>2</id>
<age>40</age>
<sex>female</sex>
</student>
<student>
<name>tom</name>
<id>32</id>
<age>31</age>
<sex>male</sex>
</student>
<message>hello world</message>
</fool>
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.