Document document = new XMLDocument();
<items>
</items>
items
)を作成するには
Element rootElement = document.createElement("items");
document.appendChild(rootElement);
<items>
<item>VALUE</item>
</items>
Element element = document.createElement("item");
rootElement.appendChild(element);
item
)を追加します。
<items>
<item></item>
</items>
<items>
<item />
</items>
Text textContents = document.createTextNode("VALUE");
element.appendChild(textContents);
VALUE
)を追加します。
<items>
<item>VALUE</item>
</items>
appendChild()
というメソッドを使用しましたが、任意に位置に要素を追加する場合は、
Node insertChild(Node newChild, Node refChild) throws DOMException;
id
)を追加して
<items>
<item id="ID">VALUE</item>
</items>
Attribute attribute = document.createAttribute("id");
attribute.setValue("ID");
element.setAttributeNode(attribute);
element.setAttribute("id", "ID");
Element cloneElement = element.clone(true);
cloneElement.getChildNodes().item(0).setNodeValue("VALUE2");
cloneElement.setAttribute("id", "ID2");
rootElement.appendChild(cloneElement);
import org.w3c.dom.*;
class Foo {
public void bar() {
try {
// XML文書を作成する
Document document = new XMLDocument();
// ルート要素を追加する
Element rootElement = document.createElement("items");
document.appendChild(rootElement);
// 子要素を追加する
Element element = document.createElement("item");
rootElement.appendChild(element);
// 値を設定する
Text textContents = document.createTextNode("VALUE");
element.appendChild(textContents);
// 属性を追加する
element.setAttribute("id", "ID");
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
<items>
<item id="ID">VALUE</item>
</items>
Attr attribute = document.createAttribute("AttributeName");
CDATASection cdataSection = document.createCDATASection("DataString");
Comment comment = document.createComment("CommentString");
DocumentFragment documentFragment = document.createDocumentFragment();
Elenment element = document.createElement("ElementName");
EntityReference entityReference = document.createEntityReference("EntityReferenceName");
ProcessingInstruction processingInstruction = document.createProcessingInstruction("ProcessingInstructionName", "DataString");
Text text = document.createTextNode("TextString");
NodeList nodeList = node.getChildNodes();
int length = nodeList.getLength();
i
番目のノードを取得する
Node node = nodeList.item(i);
Node node = nodeList.getFirstChild();
Node node = nodeList.getLastChild();
Node node = currentNode.getPreviousSibling();
Node node = currentNode.getNextSibling();
Node parentNode = node.getParentNode();
boolean stat = node.hasChildNodes();
Document document = node.getOwnerDocument();
NodeList nodeList = node.getElementByTagName("hoge");
hoge
)の要素の一覧を取得することもできます。
NamedNodeMap nodeMap = element.getAttributes();
int length = nodeMap.getLength();
i
番目の属性を取得する
Node node = nodeMap.item(i);
short nodeType = node.getNodeType();
String nodeName = node.getNodeName();
String nodeValue = node.getNodeValue();
ノードタイプ | 定義値 | getNodeName | getNodeValue | getNodeAttribute |
---|---|---|---|---|
要素 | Node.ELEMENT_NODE | 要素名 | null | NamedNodeMap |
属性 | Node.ATTRIBUTE_NODE | 属性名 | 属性値 | null |
テキスト | Node.TEXT_NODE | #text | テキスト | null |
CDATAセクション | Node.CDATA_SECTION_NODE | #cdata-section | CDATAセクションの内容 | null |
実体参照 | Node.ENTITY_REFERENCE_NODE | 展開される実体参照 | null | null |
実体 | Node.ENTITY_NODE | 実体名 | null | null |
処理命令 | Node.PROCESSING_INSTRUCTION_NODE | ターゲット | ターゲットを除いた内容全部 | null |
コメント | Node.COMMENT_NODE | #comment | コメントの内容 | null |
ドキュメント | Node.DOCUMENT_NODE | #document | null | null |
文書型 | Node.DOCUMENT_TYPE_NODE | 文書型名 | null | null |
部分文書 | Node.DOCUMENT_FRAGMENT_NODE | #document-fragment | null | null |
記法 | Node.NOTATION_NODE | 記法名 | null | null |
String attributeName = attribute.getName();
String attributeValue = element.getAttribute(attributeName);
String attributeValue = attribute.getValue();
Element element = attribute.getOwnerElement();
import org.w3c.dom.*;
import javax.xml.parsers.*;
class Foo {
public void scanNodes(NodeList nodeList) throws Exception {
try {
for ( int i=0; i<nodeList.getLength(); i++ ) {
Node node = nodeList.item(i);
String nodeName = node.getNodeName();
String nodeValue = node.getNodeValue();
if ( Node.ELEMENT_NODE == node.getNodeType() ) {
NamedNodeMap nodeMap = node.getAttributes();
it ( null != nodeMap ) {
for ( int j=0; j<nodeMap.getLength(); j++ ) {
String attributeName = nodeMap.item(j).getNodeName();
String attributeValue = nodeMap.item(j).getNodeValue();
}
}
}
if ( true == node.hasChildNodes() ) {
scanNodes( node.getChildNodes() );
}
}
} catch (Exception e) {
System.out.println(e.getMessage());
e.printStackTrace();
throw e;
}
}
}
parentNode.appendChild(element);
element.getChildNodes().item(0).setNodeValue(value);
parentNode.replaceChild(newElement, oldElement);
parentNode.removeChild(element);
attribute.setValue(attributeValue);
element.setAttribute(attributeName, attributeValue);
element.setAttributeNode(attributeNode);
element.removeAttribute(attributeName);
element.removeAttributeNode(attributeNode);
((XmlDocument)document).write(出力先, エンコード);
BufferdWriter
を使うとしたら
import org.w3c.dom.*;
import javax.xml.parsers.*;
import java.io.*;
FileWriter fileWriter = new FileWriter("hoge.xml");
BufferedWriter bufferdWriter = new BufferedWriter(fileWriter);
((XmlDocument)document).write(bufferdWriter, "Shift_JIS");
bufferdWriter.flush();
bufferdWriter.close();
import org.w3c.dom.*;
import javax.xml.parsers.*;
import java.io.*;
File fileObject = new File("hoge.xml");
DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document document = docBuilder.parse(fileObject);
document.normalize();
import org.w3c.dom.*;
import javax.xml.parsers.*;
import java.io.*;
import java.net.*;
URL url = new URL("http://www.aheahe.com/xml/hoge.xml");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setDoOutput(true);
connection.setUseCashes(false);
connection.setRequestMethod("GET"); または connection.setRequestMethod("POST");
InputStream inputStream = connection.getInputStream();
DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document document = docBuilder.parse(inputStream);