我需要使用Java方法打印选择父标记的子标记名称。在下面给出的Xml文件中。我要选择一个父标记 subject ,我打算在主题父标记内打印每个子和SubChild标记。例如:div,p,LiberaryBook,p,p,textfile,textfile,textfile 。 2.它应该打印包含textfile标签的p标签内的文本 1.如何从选择的父标签打印子标签名称?并打印如何在p标签内打印包含textfile标签的文本?
<parent>
<Body class="student" id="181_student_method_3">
<Book class="Book_In_School_11" id="181_student_method_11"/>
<subject class="subject_information " id="181_student_subject_12"/>
<div class="div_passage " id="181_div_method_3">
<p class=" p_book_name" id="181_paragraph_13">
Best Java
<LiberaryBook class="Liberary" id="181_Liberary_9" >
<span class="p_span_name" id="181_span_13">Hello</span>
</LiberaryBook>
Java Program
</p>
<p class=" p_book_name" id="181_paragraph_13">
World
</p>
<p class="p_book_name" id="181_paragraph_14">
<textfile class="choice" id="C_10">Java.</textfile>
<textfile class="choice" id="C_11">
Find out how you rate against top coders.
</textfile>
<textfile class="choice3 " id="choice_12">
Unlock awesome startup jobs and hacker deals.
</textfile>
<textfile class="choice4 " id="choice_13">
User Id friends.
</textfile>
</p>
</div>
</subject>
</Body>
</parent>
private static void deleteElement(Document someNode) {
NodeList parentNode = someNode.getElementsByTagName("hottextInteraction").item(0).getChildNodes();
int parentNodeContentSize = parentNode.getLength();
for (int i = 0; i < parentNodeContentSize; i++) {
Node parentNodeitem = parentNode.item(i);
System.out.println("ChildList"+((Node) parentNodeitem.getChildNodes()).getNodeName());
NamedNodeMap attributes = parentNodeitem.getAttributes();
String attributeInParent = attributes != null ? (attributes
.getNamedItem("class") != null ? attributes.getNamedItem(
"class").getNodeValue().trim() : null) : null;
if (attributeInParent != null && attributeInParent.equals("div_passage")) {
NodeList stemContent = parentNodeitem.getChildNodes();
for(int j=0;j<stemContent.getLength();j++){
Node stem = stemContent.item(j);
if(stem instanceof Element && (stem.getNodeName().equals("p"))){
NodeList parentNode1 = someNode.getElementsByTagName("p");//First try.item(0).getChildNodes();
int parentNodeContentSize1 = parentNode1.getLength();
System.out.println(parentNodeContentSize1);
for (int i1 = 0; i1 < parentNodeContentSize1;) {
Node node = parentNode1.item(i1);
if (node instanceof Element) {
Element childElement = (Element) node;
System.out.println("tag name: " + childElement.getTagName());
}i1++;
System.out.println("Sub Child Tag:"+node.getFirstChild().getNodeName());*/
}
}
}
}
}
}
答案 0 :(得分:0)
Please Check with this code to get Child Tag name and get it's text content inside P tag
package com.video.convertor;
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class Hottext {
public static void main(String[] args) {
String filePath = "/Users/myXml/check/test.xml";
File xmlFile = new File(filePath);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder;
try {
dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(xmlFile);
doc.getDocumentElement().normalize();
//Element root = doc.getDocumentElement();
System.out.println(deleteElement(doc));
doc.getDocumentElement().normalize();
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File("/Users/myXml/DEMO4.xml"));
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.transform(source, result);
System.out.println("XML file updated successfully");
} catch (SAXException | ParserConfigurationException | IOException | TransformerException e1) {
e1.printStackTrace();
}
}
private static String deleteElement(Document someNode) {
String contents = "";
NodeList itemBody = someNode.getElementsByTagName("subject").item(0).getChildNodes();
int itemBodyContentSize = itemBody.getLength();
for (int i = 0; i < itemBodyContentSize; i++) {
Node passage = itemBody.item(i);
NamedNodeMap attributes = passage.getAttributes();
String selectText = attributes != null ? (attributes
.getNamedItem("class") != null ? attributes.getNamedItem(
"class").getNodeValue() : null) : null;
if (selectText != null && selectText.toString().trim().equals("div_passage")) {
NodeList pasageElements = passage.getChildNodes();
int length = pasageElements.getLength();
boolean isPassageContent = false;
for(int j=0;j<length;j++){
Node passageContent = pasageElements.item(j);
if(passageContent instanceof Element && (passageContent.getNodeName().equals("p"))){
if(isPassageContent){
NodeList p = pasageElements.item(j).getChildNodes();
for (int k = 0;k < p.getLength(); k++){
Node noded = p.item(k);
if (noded.getNodeType() == Node.ELEMENT_NODE) {
if(noded.getNodeName() != null && (noded.getNodeName().equals("textfile"))){
contents = contents+p.item(k).getTextContent()+"\n";
}
}
}
}else{
isPassageContent = true;
}
}
}
}
}return contents;
}
}