我有一个简单的xml字符串
public class ExamSubjectsActivity extends BaseActivity {
private ActivityExamsSubjectsBinding binding;
LinearLayout myLayout;
MotionEvent mMotionEvent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.activity_exams_subjects);
myLayout = binding.grades;
myLayout.setOnTouchListener(new OnSwipeTouchListener(this) {
public void onSwipeTop() {
Toast.makeText(ExamSubjectsActivity.this, "top", Toast.LENGTH_SHORT).show();
}
public void onSwipeRight() {
Toast.makeText(ExamSubjectsActivity.this, "right", Toast.LENGTH_SHORT).show();
for (int i = 0; i < myLayout.getChildCount(); i++) {
View view = myLayout.getChildAt(i);
Rect outRect = new Rect(view.getLeft(), view.getTop(), view.getRight(), view.getBottom());
if (outRect.contains((int) mMotionEvent.getX(), (int) mMotionEvent.getY())) {
view.setBackgroundColor(ContextCompat.getColor(ExamSubjectsActivity.this, R.color.prime_orange));
}
}
}
public void onSwipeLeft() {
Toast.makeText(ExamSubjectsActivity.this, "left", Toast.LENGTH_SHORT).show();
for (int i = 0; i < myLayout.getChildCount(); i++) {
View view = myLayout.getChildAt(i);
Rect outRect = new Rect(view.getLeft(), view.getTop(), view.getRight(), view.getBottom());
if (outRect.contains((int) mMotionEvent.getX(), (int) mMotionEvent.getY())) {
view.setBackgroundColor(ContextCompat.getColor(ExamSubjectsActivity.this, R.color.white));
}
}
}
public void onSwipeBottom() {
Toast.makeText(ExamSubjectsActivity.this, "bottom", Toast.LENGTH_SHORT).show();
}
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
});
}
如果没有xmlns =“”,我可以获取值,但是当xmlns属性出现时,它不返回值
这是我的代码
<Detail>
<firstname xmlns=""></firstname >
<lastname xmlns=""></lastname>
</Detail>
我如何获得名字值。我不想将DocumentBuilder类用于这个单个元素
答案 0 :(得分:0)
我认为最简单的方法是在没有显式解析XML文本的情况下使用JAXB,如下所示。创建一个Detail类,该类仅注释要从输入XML加载的变量。
对于 input.xml 文件,该文件不仅包含firstname和lastname,还包含其他XML元素(如注释中所请求的):
<?xml version="1.0" encoding="UTF-8"?>
<Detail>
<firstname>firstname</firstname>
<lastname>lastname</lastname>
<elementA>element A</elementA>
<elementB>element B</elementB>
<elementC>element C</elementC>
</Detail>
如果你有一个详细信息类,只注释了firstname和lastname,如下所示:
package test;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.NONE)
@XmlRootElement(name = "Detail")
@XmlType(propOrder = { "firstname", "lastname"})
public class Detail {
@XmlElement(name="firstname")
private String firstname;
@XmlElement(name="lastname")
private String lastname;
private String elementA = "default elementA";
private String elementB = "default elementB";
private String elementC = "default elementC";
public Detail(){}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getElementA() {
return elementA;
}
public void setElementA(String elementA) {
this.elementA = elementA;
}
public String getElementB() {
return elementB;
}
public void setElementB(String elementB) {
this.elementB = elementB;
}
public String getElementC() {
return elementC;
}
public void setElementC(String elementC) {
this.elementC = elementC;
}
}
以及 jaxb.index 文件,与编译类位于同一文件夹中,其唯一内容为:
Detail
您测试输入XML ,如下所示:
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
public class Test {
public static void main(String[] args) {
try{
JAXBContext jc = JAXBContext.newInstance(Detail.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("input.xml");
Detail detail = (Detail) unmarshaller.unmarshal(xml);
System.out.println("Detail firstname:"+detail.getFirstname()+" lastname:"+detail.getLastname());
}
catch(JAXBException e){
System.out.println(e.getMessage());
}
}
}
您将看到Detail仅从XML文件加载了firstname和lastname。