-1
A
回答
0
1. try to take the 2 example
for a number use this regex
\d-->Any digit, short for [0-9]
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
try {
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse (new File("c:\\tmp\\my.xml"));
// normalize text representation
doc.getDocumentElement().normalize();
System.out.println ("Root element of the doc is " + doc.getDocumentElement().getNodeName());
NodeList listOfBooks = doc.getElementsByTagName("book");
int totalBooks = listOfBooks.getLength();
System.out.println("Total no of books : " + totalBooks);
for(int i=0; i<listOfBooks.getLength() ; i++){
Node firstBookNode = listOfBooks.item(i);
if(firstBookNode.getNodeType() == Node.ELEMENT_NODE){
Element firstElement = (Element)firstBookNode;
System.out.println("Year :"+firstElement.getAttribute("year"));
//-------
NodeList firstNameList = firstElement.getElementsByTagName("title");
Element firstNameElement = (Element)firstNameList.item(0);
NodeList textFNList = firstNameElement.getChildNodes();
System.out.println("title : " +
((Node)textFNList.item(0)).getNodeValue().trim());
}
}//end of for loop with s var
}catch (SAXParseException err) {
System.out.println ("** Parsing error" + ", line "
+ err.getLineNumber() + ", uri " + err.getSystemId());
System.out.println(" " + err.getMessage());
}catch (SAXException e) {
Exception x = e.getException();
((x == null) ? e : x).printStackTrace();
}catch (Throwable t) {
t.printStackTrace();
}
}
///Regex example
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexMatches
{
public static void main(String args[]){
// String to be scanned to find the pattern.
String line = "This order was places for QT3000! OK?";
String pattern = "(.*)(\\d+)(.*)";
// Create a Pattern object
Pattern r = Pattern.compile(pattern);
// Now create matcher object.
Matcher m = r.matcher(line);
if (m.find()) {
System.out.println("Found value: " + m.group(0));
System.out.println("Found value: " + m.group(1));
System.out.println("Found value: " + m.group(2));
} else {
System.out.println("NO MATCH");
}
}
}
相關問題
- 1. 正則表達式的XML標籤
- 2. 在Java中使用正則表達式提取XML標籤的內容
- 3. Java的正則表達式替換XML標籤名
- 4. 正則表達式#標籤
- 5. 正則表達式,標籤
- 6. 使用正則表達式匹配xml標籤名稱
- 7. 如何使用正則表達式解析XML類標籤
- 8. 在java中使用正則表達式
- 9. 使用正則表達式在Java中
- 10. 在Java中使用正則表達式中的HTML標籤的風格
- 11. 在Python中使用正則表達式的html標籤
- 12. 使用正則表達式在java中的數學表達式?
- 13. 正則表達式中的正則表達式標籤無法正常工作
- 14. 使用正則表達式中的Java
- 15. 正則表達式替換標籤在C#中的標籤?
- 16. 兩個標籤用正則表達式
- 17. 正則表達式引用標籤php
- 18. 正則表達式,用於從特定的XML標籤
- 19. 正則表達式來解析模板標籤中的XML
- 20. 正則表達式查找不完整的xml標籤在c#
- 21. 在Java中使用正則表達式解析HTTP XML響應
- 22. 在java中使用正則表達式來修改xml
- 23. Java的正則表達式爲xml
- 24. 正則表達式來提取標籤 - 值對在Java中
- 25. 標題標籤與正則表達式
- 26. 如何使用Java正則表達式去除一些XML標籤及其中的所有標籤/數據
- 27. 使用正則表達式在「標籤」中提取MySQL數據?
- 28. 如何在正則表達式中使用Java lambda表達式
- 29. 在java中使用正則表達式分隔自定義標籤的內容
- 30. 檢索標籤的正則表達式
使用XML解析器... – fge
什麼樣'' 元素? –
Pshemo